2010-07-31 38 views
0

我有一個空白的用戶控件(子),在Page_Load期間我創建了一些文本框和一個按鈕。我還爲該按鈕添加了一個事件。手柄按鈕單擊來自用戶控件動態加載的事件

然後,我從另一個用戶控件(父級)的佔位符中動態加載該用戶控件。 這兩個控件都呈現正確,但是當我點擊按鈕時,事件不會被解僱。 關於如何處理活動的任何想法?

兒童代碼:

protected void Page_Load(object sender, EventArgs e) 
{  
    /*... other user controls ..*/ 
    UpdateButton = new LinkButton(); 
    UpdateButton.ID = "buttonid"; 
    UpdateButton.Text = "text"; 
    UpdateButton.Click += new EventHandler(UpdateButton_Click); 
    this.Controls.Add(UpdateButton); 
} 

protected override void Render(HtmlTextWriter writer) 
{ 
    for (int i = 0; i < this.Controls.Count; i++) 
    { 
     this.Controls[i].RenderControl(writer); 
    } 
} 

public void UpdateButton_Click(object sender, EventArgs e) 
{ 
    //Do stuff 
} 

父代碼:

protected void Page_Load(object sender, EventArgs e) 
{  
    placeholder.Controls.Clear(); 
    ChildControl uc = (ChildControl)LoadControl("~/UserControls/ChildControl.ascx"); 
    placeholder.Controls.Add(uc); 
} 

如果我使用的子控件directy(即沒有家長控制)的點擊事件引發,並很好地處理。但是當我使用父控件時,調試器甚至不會在UpdateButton_Click()內打一個斷點。

在此先感謝。

+0

當使用父母和孩子時,兒童的「Page_Load」是否正確執行? – 2010-07-31 16:06:10

+0

是的,它的確如此。子控件內的所有控件(文本框,按鈕,標籤)都可以正確呈現,並且子控件可以正常工作。 – 2010-07-31 16:14:37

回答

0

我想我知道可能發生了什麼。在您的父母Page_Load中,您打電話給placeholder.Controls.Clear();這就是您想象並清除控件的事情,包括髮生的任何事件。刪除此行時會發生什麼?您是否在每次回傳中獲得了額外的一個?

+0

我刪除了該行,並且每回發只獲得一個子控件,但我仍無法捕捉該事件。 – 2010-07-31 18:28:00

相關問題