all groups > asp.net webcontrols > november 2003 >
You're in the

asp.net webcontrols

group:

Adding Dynamic CheckBox Control State Problem



Adding Dynamic CheckBox Control State Problem imranish NO[at]SPAM hotmail.com
11/29/2003 2:28:20 PM
asp.net webcontrols: 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
Re: Adding Dynamic CheckBox Control State Problem Teemu Keiski
11/30/2003 5:32:59 PM
For CheckBoxes to keep state (load postback data), they should be added at
Page_Load at the latest.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist


[quoted text, click to view]

AddThis Social Bookmark Button