How to get selected values from a dynamically created DropDownList Array after PostBack (Button Click)

Posted by user739280 on Stack Overflow See other posts from Stack Overflow or by user739280
Published on 2011-05-05T06:39:16Z Indexed on 2012/09/20 3:38 UTC
Read the original article Hit count: 647

Filed under:
|
|
|
|

I have a CheckBoxList that contains Employee Names on a Wizard Step. When employees are selected and the active step is changed, the Wizard1_ActiveStepChanged function is called and it dynamically creates a DropDownList Array for each employee that is selected. Each DropDownList specifies a condition of the employee. The DropDownList is created properly. When the user clicks submit, the DropDownList array is deleted and no selected values can be pulled from the array.

I understand this is an issue with the PostBack and can be fixed with ViewState, but I am trying to figure out what I can do to fix it. ViewState is enabled for the checkboxlist and the DropDownList.

This is what I have in the body of my System.Web.UI.Page class

    private int empcount;
    private DropDownList[] DDL_Emp
    {
        get
        {
           return (DropDownList[])ViewState["DDL_Emp"];
        }
        set
        {
            ViewState["DDL_Emp"] = value;
        }
    }

The relevant code:

    protected void Wizard1_ActiveStepChanged(object sender, EventArgs e)
    {

        if (Request.QueryString["type"] == "Accident"  && BulletedList1.Items.Count > 0)
        {
            this.empcount = 0;

            for (int i = 0; i < CBL_EmpInvolved.Items.Count; i++)
            {
                if (CBL_EmpInvolved.Items[i].Selected)
                {
                    this.empcount++;
                }

            }

            if(this.empcount > 0)
            {
                this.DDL_Emp = new DropDownList[this.empcount];                   



                for (int i = 0, j=0; i < CBL_EmpInvolved.Items.Count; i++) 
                {
                    if (CBL_EmpInvolved.Items[i].Selected)
                    {
                        List<ListItem> cond = new List<ListItem>();

                        cond.Add(new ListItem("Disabled", CBL_EmpInvolved.Items[i].Value));
                        cond.Add(new ListItem("Diseased - Fatality", CBL_EmpInvolved.Items[i].Value));                          
                        cond.Add(new ListItem("On Treatment - Short Term Disability", CBL_EmpInvolved.Items[i].Value));
                        cond.Add(new ListItem("On Treatment - Long Term Disability", CBL_EmpInvolved.Items[i].Value));
                        cond.Add(new ListItem("Treated - Back to Work", CBL_EmpInvolved.Items[i].Value));
                        cond.Add(new ListItem("Treated - Relocated", CBL_EmpInvolved.Items[i].Value));
                        cond.Add(new ListItem("Treated - Transferred", CBL_EmpInvolved.Items[i].Value));

                        this.DDL_Emp[j] = new DropDownList();

                        this.DDL_Emp[j].ID = "DD_LabCondition_" + CBL_EmpInvolved.Items[i].Value;
                        this.DDL_Emp[j].EnableViewState = true;
                        this.DDL_Emp[j].Visible = true;
                        this.DDL_Emp[j].Items.AddRange(cond.ToArray());
                        this.DDL_Emp[j].Items.Insert(0, new ListItem("-- Select condition of employee: " + CBL_EmpInvolved.Items[i].Text, ""));


                        PH_LabCondition.Controls.Add(this.DDL_Emp[j]);
                        j++;
                    }

                }

                PH_LabCondition.Visible = true;
                MV_LabCondition.Visible = true;

                Label1_ReportTitle.Text += "Control Count: " + PH_LabCondition.Controls.Count.ToString();

            }

            MV_LabCondition.ActiveViewIndex = 1;
            MV_LostTime.ActiveViewIndex = 1;
        }
    }

This code is giving me the following error now:

Type 'System.Web.UI.WebControls.DropDownList' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

I've tried changing buttons to images, playing with the AutoPostBack feature. I'm lost on how to get my dropdownlist array saved to the ViewState and accessing it after the postback.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about controls