Adding Dynamic CheckBox Control State Problem

Discussion in 'ASP .Net Web Controls' started by Imran Masud, Nov 29, 2003.

  1. Imran Masud

    Imran Masud Guest

    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
    Imran Masud, Nov 29, 2003
    #1
    1. Advertising

  2. Imran Masud

    Teemu Keiski Guest

    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


    "Imran Masud" <> wrote in message
    news:...
    > 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
    Teemu Keiski, Nov 30, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vikram
    Replies:
    3
    Views:
    5,767
    Karl Seguin
    Apr 12, 2005
  2. Doogie
    Replies:
    1
    Views:
    460
    Doogie
    Jun 14, 2007
  3. Imran Aziz

    checkbox value attribute in checkbox control ?

    Imran Aziz, Aug 6, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    222
    Imran Aziz
    Aug 8, 2005
  4. pizzy
    Replies:
    0
    Views:
    154
    pizzy
    Mar 23, 2005
  5. pizzy
    Replies:
    5
    Views:
    372
    pizzy
    Apr 12, 2005
Loading...

Share This Page