2012-10-02 156 views
0

我有一個簡單的webpart,下面的代碼,但是當我點擊「UnAcceptClick」中的按鈕代碼時無法工作。OnClick事件在WebPart中不起作用

我失蹤或做錯了什麼?

public class simple_wp : WebPart 
{ 

    protected override void OnPreRender(EventArgs e) 
    { 
     base.OnPreRender(e); 

     CreateControlHierarchy(); 
    } 

    private void CreateControlHierarchy() 
    { 

       unAccept.Text = "Cancel"; 
       unAccept.Click += new EventHandler(UnAcceptClick); 

    ... some other code ... } 


    private void UnAcceptClick(object sender, EventArgs e) 
    { 
     ... some code ... 
    } 

    protected override void CreateChildControls() 
    { 
     try 
     { 
      Controls.Add(unAccept); // button 
     } 
     catch (Exception ex) 
     { 
      Controls.Add(new LiteralControl(ex.ToString())); 
     } 

    } // # CreateChildControls # 
} 

回答

1

CreateChildControls添加這個,你在你的init方法關聯不渲染應用的

protected override void CreateChildControls() 
    { 
     try 
     { 
      unAccept.Click += new EventHandler(UnAcceptClick); 
      Controls.Add(unAccept); // button 
     } 
     catch (Exception ex) 
     { 
      Controls.Add(new LiteralControl(ex.ToString())); 
     } 

    } // 
+0

謝謝很多=)你的答案的幫助,現在代碼工作=) – Nana