Adding Dynamic CheckBox Control State Problem

I

Imran Masud

Hi,
I am trying to add a checkbox dynamically in reply to a button click.
Each button click will add a new checkbox object to an ArrayList
Object. I store this ArrayList object in the session and populate it.

private void Button1_Click(object sender, System.EventArgs e){
CheckBox cb = new CheckBox();
cb.ID = cb.GetType().ToString()+((char)(checkList.Count+64)).ToString();
cb.Text = ((char)(checkList.Count+64)).ToString();
cb.AutoPostBack =true;
this.checkList.Add(cb); // ArrayList object
}

I have overloaded OnPreRender method and add the checkbox to my user
control as shown in the code below.

protected override void OnPreRender(EventArgs e)
{
foreach (CheckBox cb in checkList)
{
this.Controls.Add(cb);
}
base.OnPreRender (e);
}

My problem is the state of the checkboxes are not maintained. If I
have added
2 checkboxes and then I click on them, and afterwards I click the
button again
a new checkbox will be added but 1st two be become unchecked.

I will be really grateful, if any of webcontrol guru's outthere
can help me with this problem.

Regards
Imran
 
T

Teemu Keiski

For CheckBoxes to keep state (load postback data), they should be added at
Page_Load at the latest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top