onserverclick在运行时添加属性时不触发c#

时间:2016-08-23 06:34:01

标签: c# html asp.net twitter-bootstrap visual-studio

我在C#网络表单中在运行时添加了一个按钮。我需要在调用按钮时调用函数btnEdit_click。不知怎的,这个功能没有被调用。

代码如下。请帮忙

        protected void btnEdit_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Setup.aspx");
    }

                HtmlGenericControl EditButton = new HtmlGenericControl("button");
                EditButton.Attributes["class"] = "btn btn-default";
                EditButton.Attributes["id"] = "editButton";
                EditButton.Attributes["runat"] = "server";
                EditButton.Attributes["OnServerClick"] = "btnEdit_Click";
                EditButton.InnerText = "Edit";

1 个答案:

答案 0 :(得分:1)

  <html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
  <title></title>
  </head>
  <body>
     <form id="form1" runat="server">
     </form>
  </body>
  </html>



    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlButton EditButton = new HtmlButton();
        EditButton.Attributes["class"] = "btn btn-default";
        EditButton.Attributes["id"] = "editButton";
        EditButton.Attributes["runat"] = "server";
        EditButton.InnerText = "Edit";
        EditButton.ServerClick += btnEdit_Click;

        this.form1.Controls.Add(EditButton);
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Setup.aspx");
    }