2016-08-23 78 views
1

我在C#web窗體的運行時添加一個按鈕。當按鈕被調用時,我需要調用一個函數btnEdit_click。不知何故該函數沒有被調用。onserverclick在運行時添加屬性時不會觸發c#

代碼如下。請幫助

 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
<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"); 
    } 
+0

我想通了一段時間回來,但它的工作原理。謝謝!! – Zee

相關問題