简体   繁体   English

复选框已更改事件未触发动态添加控件

[英]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.

相关问题 动态添加的用户控件事件未触发 - Dynamically added user control event not firing 动态添加的用户控件中的文本框上的TextChanged事件未触发 - TextChanged event on textbox in dynamically added user control not firing 动态添加的事件处理程序未触发 - Dynamically Added Event Handler Not Firing 动态添加的DropDownlists不会触发SelectedIndexChanged事件 - Dynamically Added DropDownlists Are Not Firing SelectedIndexChanged Event 将Checked Changed Event添加到复选框时出错-使用c#将其动态添加到asp.net应用程序中的Gridview - Error in adding Checked Changed Event to a checkbox- which is added dynamically to a gridview in asp.net application using c# 在事件期间访问动态添加到占位符的控件 - Accessing a control dynamically added to a placeholder during an event 动态添加控件的事件处理程序不起作用 - Event handler for dynamically added control does not work 动态添加的ImageButton Command事件直到第二次单击后才会触发 - Dynamically added ImageButton Command event not firing until second click 为什么我动态添加的事件处理程序无法持续触发 - Why is my dynamically added event handler not firing consistently C#ASP.NET Wrap通过动态添加的Label控件动态添加了CheckBox控件 - C# ASP.NET Wrap dynamically added CheckBox Control with a dynamically added Label Control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM