注册 登录
编程论坛 ASP.NET技术论坛

如何在客户端触发服务器端控件的事情?

foshan 发布于 2007-05-30 12:38, 634 次点击

如何在客户端触发服务器端控件的事情?
以下的代码是要实现:通过点击Button1控件,通过客户端的javascript激发Button2控件的click事件,但不成功。
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function AA()
{
document.all("Button2").click();
return confirm("aa");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" OnClientClick="return AA();"/>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button2" /></div>
</form>
</body>
</html>


protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "AA");
}
protected void Button2_Click(object sender, EventArgs e)
{
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this.Button2, this.Button1.GetType(), "ajs", "alert('不正确!');", true);

}
protected void Button1_Click(object sender, EventArgs e)
{

}

5 回复
#2
冰残剑2007-05-30 12:40
要采用无刷新技术才行
#3
川流不息2007-05-30 13:55
protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "AA");//AA();AA後面加上括號和分號就行了。
}
#4
foshan2007-05-30 14:12

斑竹,试过不行,仍然不能显示“不正确”的提示,只能显示“aa”的提示……

#5
川流不息2007-05-30 14:20
document.all("Button2").click();

document.all.Button2.click();---->再試試
#6
rainic2007-05-30 14:38

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" OnClientClick="return AA();"/>

打红色的去掉


document.all("Button2").click();
改成
document.getElementById("Button2").click();

1