1

Am using the below code to create a multiple drop down list. But i can't able to maintain the previous selected value. Please help me to maintain the previous vaues.

My code is Here:

        for (int i = 1; i <= Count; i++)
        {
            Session["i"] = Count;

            Panel1.Controls.Add(new LiteralControl("<br />"));
            Label lbl = new Label();
            lbl.ID = "lbl" + i;
            lbl.Text = "Head";
            Panel1.Controls.Add(lbl);
            Panel1.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));

            DropDownList ddl = new DropDownList();
            ddl.ID = "ID" + i;
            ddl.DataValueField = "fld_Head";
            ddl.DataTextField = "fld_Head";
            ddl.DataSource = DVS;
            ddl.DataBind();
            Panel1.Controls.Add(ddl);
        }
Johnny_D
  • 4,592
  • 3
  • 33
  • 63
romi
  • 611
  • 1
  • 9
  • 16

3 Answers3

3

As I already mentioned here you should init values of you dynamic controls in Page_Init event.

Because asp.net internal functionality working with viewstate populates values in Page_Load event and if your dynamic control isn't still created it's values are ignored.\

Common issue =)

Community
  • 1
  • 1
Johnny_D
  • 4,592
  • 3
  • 33
  • 63
0

You are adding a DropDownList dynamically to a Panel. Newly added DropDownList control will not be available at a post-back. You can use

HttpContext.Current.Request.Form["ID" + i]

to get the values from post request. And with that values you can add new controls with new state.

Yiğit Yener
  • 5,796
  • 1
  • 23
  • 26
0

Putting the creation of dynamic controls in Page_Init works perfectly.

jesslee
  • 13
  • 4