日期:2014-05-18  浏览次数:20478 次

asp.net 在一个button服务端事件中调用另一个button的客户端事件
protected void Button1_Click(object sender, EventArgs e)
{
 Button2.Attributes.Add("onclick", "return confirm('是否确认执行?');");
ScriptManager.RegisterClientScriptBlock(this, typeof(string), "AddItem",<script>document.getElementById('"+Button2.ClientID+"').click()</script>", true);
}

但是这段代码执行未弹出确认框

------解决方案--------------------
完整的例子
HTML code
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  protected void Button1_Click(object sender, EventArgs e)
  {
    Button2.OnClientClick = "return confirm('是否确认执行?');";
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "x", "document.getElementById('" + Button2.ClientID + "').click()", true);
  }

  protected void Button2_Click(object sender, EventArgs e)
  {
    Response.Write("Button2_Click执行了");
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
</head>
<body>
  <form runat="server">
  <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button1" />
  <asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click" />
  </form>
</body>
</html>