我有一個包含一個複選框和一個TextBox一個用戶控件:添加CheckChanged事件處理程序複選框內動態添加用戶控件
<asp:CheckBox runat="server" ID="chk1" />
<asp:TextBox runat="server" ID="tb1" />
在Page_Load中,我將其中幾個動態他們在一個小組頁:
//loop through the results from DB
foreach (Thing t in Things)
{
//get the user control
MyUserControl c1 = (MyUserControl)Page.LoadControl("~/UserControls/MyUserControl.ascx");
//set IDs using public properties
c1.ID = "uc" + t.ID;
c1.CheckBoxID = "chk" + t.ID;
cl.TextBoxID = "tb" + t.ID;
//add it to the panel
myPanel.Controls.Add(c1);
//add the event handler to the checkbox
((CheckBox)myPanel.FindControl(c1.ID).FindControl(c1.CheckBoxID)).CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
}
然後我創建的方法在同一頁的事件處理程序:
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
string test = "breakpoint here";
}
當我在CheckBox_CheckedChanged中放置一個斷點時,當我的複選框被點擊時它從未被打中。
當我看着查看源代碼,這是獲取生成的代碼:
<input id="ctl00_body_uc1_chk1" type="checkbox" name="ctl00$body$uc1$chk1" checked="checked" />
因此,它似乎並沒有被拿起時,我添加事件處理。這很奇怪,因爲它可以提取所有其他的東西。
我錯過了什麼嗎?
是的。就是這樣。謝謝! – 2009-10-01 13:55:19