tags:

views:

185

answers:

3

I am adding some checkboxes dynamically during runtime, and I need to know whether they are checked or not when I reload them next time. I load the checkbox values from a list stored in ViewState. The question is: when do I save or check for the value of the the Checked? I tried the event dispose for the check box and the place holder I am adding the checkboxes in, but it wasn't fired. i.e. when I put a break point it didn't stop. So any suggestions?

This is a sample code, but I don't think it is necessary:

void LoadKeywords()
    {
        bool add = true;
        foreach (string s in (ViewState["keywords"] as List<string>))
            if (s == ddlKeywords.SelectedItem.Text)
            {
                add = false;
                continue;
            }

        if (add)
            (ViewState["keywords"] as List<string>).Add(ddlKeywords.SelectedItem.Text);



        foreach (string s in (ViewState["keywords"] as List<string>))
        {
            CheckBox kw = new CheckBox();
            kw.Disposed += new EventHandler(kw_Disposed);
            kw.Text = s;
            PlaceHolderKeywords.Controls.Add(kw);
        }
    }
A: 

How to:

try adding a javascript code, that handles checked(), u can get the checkboxes by using document.findElementById(ID) , then store the checkboxe's value into a hiddenfield that has a runat="server" property.

When to: either on pageload , check if page is postback(), and check the hiddenfield(s) value(S). or add a submit button (and place its event in the code behind, runat="server" property).

hope this helps u.

Madi D.
Not a great choice if I'm honest - ASP.Net will take care of everything without the need for manual intervention *if* you respect the page life cycle
Gavin Osborn
+2  A: 

If you are dynamically adding controls at run time you have to make sure that those controls are populated to the page's Control collection before ViewState is loaded. This is so that the state of each checkbox can be rehydrated from Viewstate. The Page Load event, for example, is too late.

Typically you would dynamically add your CheckBox controls during the Init Event (before view state is loaded) and then Read the values in your Checkbox controls during the Load event (after view state is loaded).

eg:

protected override void OnInit(EventArgs e)
{
    //load the controls before ViewState is loaded
    base.OnInit(e);
    for (int i = 0; i < 3; i++)
    {
        CheckBox cb = new CheckBox();
        cb = new CheckBox();
        cb.ID = "KeyWord" + i.ToString();
        cb.Text = "Key Word"
        MyPlaceHolder.Controls.Add(new CheckBox());
    }
}

//this could also be a button click event perhaps?
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    if (Page.IsPostBack)
    {
        //read the checkbox values
        foreach(CheckBox control in MyPlaceHolder.Controls)
        {
            bool isChecked = control.Checked;
            string keyword = control.Text;

            //do something with these two values
        }
    }
}

Hope that helps

**EDIT** Forgot to mention that this is obviously just demo code - you would need to flesh it out.

For more information on dynaic control rendering in ASP.Net check out this article on 4Guys.

For more information on the page life-cycle in ASP.Net check out MSDN.aspx).

Gavin Osborn
Thanks man. I began to understand your idea, but what is populated?
Ahmad Farid
Ahmed, This code populates Checkbox controls into a PlaceHolder control - like your example does. This happens during the Page (or Control) Init method. I am just creating 3 - but you can create as many checkboxes as you like.You have to do this in the Page Init because otherwise Viewstate - will not be able to persist the CheckBox.Checked property over PostBack.Then when posting back in Page Load (after Viewstate has loaded) you can read the state of the checkboxes after a user has submitted the form (you probably have a button?).
Gavin Osborn
I have updated the code example to be a little clearer - hopefully it will make a little more sense.
Gavin Osborn
A: 

Create your control in this event, in this viewstate will automatically maintain.

protected override void CreateChildControls()
{
   LoadKeywords();
}

you should have to create your control in CreateChildControls to maintain viewstate of control during postback... this is the best approach when you creating your control at runtime.. you don't need to maintain viewstate manually.... these control treat same like other controls on the page and available.

Muhammad Akhtar