0

This is my code. The Problem is I can get the first set of checkboxes in the checbox1_checkedChanged() but not the second set. What am I doing wrong?

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
     CheckBox checkthatBox= (CheckBox)sender;
        if (checkthatBox.Checked)
        {
            for(int i=0; i<4; i++)
            {
                CheckBox ch = new CheckBox();
                ch.ID = "ch" + i;
                ch.Text = "site "+i;
                ch.CssClass = "listItemWidth";
                ch.AutoPostBack= true;
                ch.ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
                ch.CheckedChanged += ch_CheckedChanged;
                sitesPanel.Controls.Add(ch);
            }
        }
    }

    void ch_CheckedChanged(object sender, EventArgs e)
    {
        Response.Write("YO");
        CheckBox checkthatBox = (CheckBox)sender;
        if (checkthatBox.Checked)
        {
            for (int i = 0; i < 4; i++)
            {
                CheckBox ch = new CheckBox();
                ch.ID = "dl" + i;
                ch.Text = "Document Library " + i;
                ch.CssClass = "listItemWidth";
                ch.CheckedChanged += ch_CheckedChanged;
                docLibPanel.Controls.Add(ch);
            }
        }
        throw new NotImplementedException();
    }

3 Answers3

0

It seems that Second set checkboxes are re-creating late in the page life cycle.

Kumar Manish
  • 3,746
  • 3
  • 37
  • 42
0

You should create your dynamic control every time on Page_Init if you would like to access them or handle events from them after Postback.

See link below for details: http://msdn.microsoft.com/en-us/library/hbdfdyh7%28v=vs.100%29.aspx

Here you can see the same problem.

Community
  • 1
  • 1
Pavel Timoshenko
  • 721
  • 6
  • 13
0

TRY setting

EnableViewState=false

of the dynamically created control

dazzling kumar
  • 584
  • 1
  • 8
  • 17