javascript函数,是不是只有html控件可以调用,服务器控件不可以啊?
我下面的2个button,只能html button的onclick事件可以调用javascript函数,服务器button却不可以。
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>点点滴滴</title>
<script type="text/javascript">
function haha() {
alert("haha....");
var str = document.getElementById("textBox1").value;
alert(str);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="haha()" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<input type="button" value="点击我" onclick="haha()" />
</div>
</form>
</body>
</html>
我下面的2个button,只能html button的onclick事件可以调用javascript函数,服务器button却不可以。
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>点点滴滴</title>
<script type="text/javascript">
function haha() {
alert("haha....");
var str = document.getElementById("textBox1").value;
alert(str);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="haha()" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<input type="button" value="点击我" onclick="haha()" />
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
Button2.Attributes.Add("onclick", "haha()");
}
//后台调用代码
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", " haha()", true);
<head runat="server">
<title></title> <script type="text/javascript">
function beforeSubmit() {
return confirm("你确定吗?");
} function ShowMessage(msg) {
alert(msg);
}
</script> <script type="text/C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
this.btnAddByCode.Attributes.Add("onclick", "ShowMessage('Add by code')");
} protected void btnSubmit_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", @"<script type='text/javascript'>ShowMessage('You Clicked Me!');<" + "/script>");
}
</script></head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSubmit" runat="server" Text="确定" OnClick="btnSubmit_Click" OnClientClick="return beforeSubmit();" />
<asp:Button ID="btnTest" runat="server" Text="调用js函数" OnClientClick="ShowMessage('Hello')" />
<asp:Button ID="btnAddByCode" runat="server" Text="通过代码添加js函数" />
</div>
</form>
</body>