2010-10-20 38 views
1

我的代碼:出現無法添加自定義控件到的UpdatePanel

protected void Button1_Click(object sender, EventArgs e) 
    { 

     CustomControl myControl = new CustomControl(); 
     UpdatePanel1.ContentTemplateContainer.Controls.Add(myControl); 

     Button btn = new Button(); 
     UpdatePanel1.ContentTemplateContainer.Controls.Add(btn); 
    } 

點擊後新按鈕,但我的控制沒有。 CustomControl如果罰款(?),因爲我可以將其添加到.aspx文件中,並且它沒有任何問題。

我錯了什麼?

btw。沒有錯誤或警告,它只是不添加自定義控件到HTML頁面。

回答

1

我發現了一個基於相關主題的討論的解決方案。

現在是:

CustomControl myControl = new CustomControl(); 

應該是:

string controlPath = @"~/Controls/CustomControl.ascx"; 
CustomControl myControl = (CustomControl)LoadControl(controlPath);