[英]Checkbox Changed Event not firing for dynamically added control
I have created a 3 checkboxes in my jQuery accordion control dynamically in the page load event and I am also associating the CheckedChanged Event for the textbox. 我在页面加载事件中动态地在我的jQuery手风琴控件中创建了一个3个复选框,我还将文本框的CheckedChanged事件关联起来。 But the event is not firing at all. 但事件根本没有解雇。 I am not sure what is happening here. 我不确定这里发生了什么。 Please help me. 请帮我。 Thanks and appreciate your feedback. 谢谢,感谢您的反馈。
Code that I used to generate dynamic control and associate the event 我用来生成动态控件并关联事件的代码
protected void Page_Load(object sender, EventArgs e)
{
dvAccordion.Controls.Clear();
foreach (DataRow row in dataSetIP.Tables[0].Rows)
{
HtmlGenericControl tt= new HtmlGenericControl("H3");
HtmlAnchor anc= new HtmlAnchor();
HtmlGenericControl dvP= new HtmlGenericControl("DIV");
dvP.InnerHtml = row["LD"].ToString();
CheckBox chkTest = new CheckBox();
if (!Page.IsPostBack) chkTest .ID = "chk" + row["SD"].ToString();
else
{
string uniqueID = System.Guid.NewGuid().ToString().Substring(0, 5);
chkTest .ID = "chk" + uniqueID + row["SD"].ToString();
}
chkTest.Text = row["SD"].ToString();
chkTest.AutoPostBack = true;
chkTest.CheckedChanged += new EventHandler(chkTest _CheckedChanged);
chkTest.InputAttributes.Add("Value", row["ID"].ToString());
anc.Controls.Add(chkTest);
tt.Controls.Add(anc);
dvAccordion.Controls.Add(tt);
dvAccordion.Controls.Add(dvP);
}
}
But the CheckboxChanged event is not firing. 但是CheckboxChanged事件没有触发。
It's an issue of when you add the control, ViewState, and some of the lifecycle. 这是一个添加控件,ViewState和一些生命周期的问题。 Dynamically adding controls that fully participate in the whole lifecycle is a complicated subject, and without more context, it's best for you to read the Truly Understanding Dynamic Controls series. 动态添加完全参与整个生命周期的控件是一个复杂的主题,如果没有更多的上下文,最好阅读真正理解动态控件系列。
In your case, I think you're re-creating the control on the next page load after the ViewState initialization, so it doesn't know about the binding at the time it needs to queue up the call to your bound event handler. 在您的情况下,我认为您在ViewState初始化之后重新创建下一页加载的控件,因此它不需要知道绑定时需要将调用排队到绑定事件处理程序。
尝试在Page_Init()
事件中添加控件Page_Init()
在Page_Load()
事件之前触发)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.