3

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. 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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
user788312
  • 439
  • 2
  • 10
  • 22

2 Answers2

1

It's an issue of when you add the control, ViewState, and some of the lifecycle. 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.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
  • Thanks a bunch for your reply Mufasa,but I didn't get that exactly, please tell me when do you want me to recreate those controls, so that I can get the ViewState. I tried putting it in the Page_Init it didn't work either. – user788312 Sep 23 '11 at 15:15
  • @user788312 Depending on what you want to do, there are several solutions. Either provide us more context, or I suggest reading through that series as it really is the only thing that helped me figure out how to use dynamic controls correctly. And for what it's worth, I try to avoid using them when at all possible — it's usually more effort than it's worth. For example, try using a Repeater or ListView. – Jon Adams Sep 23 '11 at 15:17
  • Mufasa Thannks again. This is the context, I have a JQuery accordion which is constructed dynamically based on the no of rows returned by the DB. I also need a checkbox in every accordion row, because I need to populate the data in the grid based on the selection in the checkbox. But as those checkboxes are created dynamically inside the accordion, The checkbox changed event, what ever I am firing is not getting called at all.If that event is fired, I can check the value of checked attribute and do my DB stuff.I hope you got it,If not please do let me know.Thanks and appreciate ur feedback. – user788312 Sep 23 '11 at 15:26
  • @user788312: Sounds to me like it would be easier to use a Repeater or ListView. From your description, I don't see what's special about it that you need to create them dynamically. It's much easier to let a Repeater or ListView RowCommand handle the event. See the answers like http://stackoverflow.com/questions/1585574/use-data-in-repeater-when-checkbox-is-check-in-asp-net/1586729#1586729 or http://stackoverflow.com/questions/179938/asp-net-how-to-access-repeater-generated-form-input-elements/180307#180307 for help on how to do that. – Jon Adams Sep 23 '11 at 16:05
0

Try adding the controls in the Page_Init() event (which is fired before the Page_Load() event).

Sir Crispalot
  • 4,792
  • 1
  • 39
  • 64