Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can any body help me to add values in drop down list which is created dynamically. I have written some code for it. But when i click on button to add the value in drop down list, panel having dynamic controls shows no value in it due to post back.Here is the code:

ASPX:


XML
<form id="form1" runat="server">

        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <div>
            <asp:DropDownList ID="ddlIds" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlIds_SelectedIndexChanged">
                <asp:ListItem>Textbox</asp:ListItem>
                <asp:ListItem>Button</asp:ListItem>
                <asp:ListItem>CheckBox</asp:ListItem>
                <asp:ListItem>DropdownList</asp:ListItem>
            </asp:DropDownList>

            <asp:Panel ID="Panel1" runat="server" EnableViewState="True"></asp:Panel>
            <asp:HiddenField ID="hdnValue" runat="server" />

            <asp:UpdatePanel runat="server" ID="updpnl_pnl_controls">

                <ContentTemplate>
                    <asp:Panel runat="server" ID="pnl_items">
                        <asp:TextBox runat="server" ID="txtvalue"></asp:TextBox>
                        <asp:Button runat="server" ID="addbtn" Text="Add Item" OnClick="addbtn_OnClick" />
                        <asp:Button runat="server" ID="doneBtn" Text="Done" OnClick="doneBtn_OnClick"/>
                        <asp:HiddenField ID="hdnId" runat="server"></asp:HiddenField>
                    </asp:Panel>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger runat="server" ControlID="addbtn" />
                </Triggers>
            </asp:UpdatePanel>
        </div>
    </form>


ASPX.CS:

C#
protected void Page_Load(object sender, EventArgs e)
   {
       pnl_items.Visible = false;
       if (!IsPostBack)
       {
           create_controls();
       }
   }
   protected void ddlIds_SelectedIndexChanged(object sender, EventArgs e)
   {

       create_controls();

   }

   protected void create_controls()
   {
       switch (ddlIds.SelectedItem.Text)
       {
           case "Textbox":
               String txt = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
               string txtid = "txtBox" + txt;
               hdnValue.Value = hdnValue.Value + "^" + txtid;
               break;
           case "CheckBox":
               String chkbox = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
               string chkid = "chkBox" + chkbox;
               hdnValue.Value = hdnValue.Value + "^" + chkid;
               break;
           case "Button":
               string count = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
               string id = "BttnBox" + count;
               hdnValue.Value = hdnValue.Value + "^" + id;
               break;
           case "DropdownList":
               string ddcount = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
               string ddid = "Dropdown" + ddcount;
               hdnValue.Value = hdnValue.Value + "^" + ddid;
               break;
       }


       if (hdnValue.Value == "") return;
       var val = hdnValue.Value.Split('^');
       foreach (var ds in val)
       {
           if (ds.Contains("chk"))
           {
               var chkBox = new CheckBox { ID = ds, Text = "Test" };
               Panel1.Controls.Add(chkBox);
               var lt = new Literal { Text = "<br />" };
               Panel1.Controls.Add(lt);
           }

           else if (ds.Contains("txt"))
           {
               var txtBox = new TextBox { ID = ds };
               Panel1.Controls.Add(txtBox);
               var lt = new Literal { Text = "<br />" };
               Panel1.Controls.Add(lt);
           }
           else if (ds.Contains("Bttn"))
           {
               var btn = new Button { ID = ds, Text = "Test" };
               Panel1.Controls.Add(btn);
               var lt = new Literal { Text = "<br />" };
               Panel1.Controls.Add(lt);
           }
           else if (ds.Contains("Drop"))
           {
               var ddl = new DropDownList { ID = ds };
               pnl_items.Visible = true;
               hdnId.Value = ds;
               Panel1.Controls.Add(ddl);
               var lt = new Literal { Text = "<br />" };
               Panel1.Controls.Add(lt);
           }
       }
   }

   protected void addbtn_OnClick(object sender, EventArgs e)
   {
       var i = Panel1.Controls.Count;
       var ddl = (DropDownList)Panel1.FindControl(hdnId.Value);
       ddl.Items.Add(new ListItem(txtvalue.Text, txtvalue.Text));
       pnl_items.Visible = true;
       //throw new NotImplementedException();
   }

   protected void doneBtn_OnClick(object sender, EventArgs e)
   {
       pnl_items.Visible = false;
       hdnId.Value = string.Empty;
   }



Thanks...
Posted
Comments
VC.J 7-Oct-14 4:06am    
here you need to add javascript function onchange event and add webmethod
In web method you can create a datatable with columns ID and value
and store it in session try that you may get the result its a trick
Yash khurana 7-Oct-14 5:06am    
Can u suggest me how....
VC.J 7-Oct-14 5:59am    
its going to be tough ,you can try this new approach that Tadit provided in this solution that might resolve this issue what you need to do store the information in viewstate. I have not tried the code though but you can try that

1 solution

Refer - Retaining State for Dynamically Created Controls in ASP.NET applications[^].
Quote:
When dynamically adding controls to an ASP.NET page in runtime the object references are lost at postback because they have no handle in the codebehind. The values entered by the user is not availible when accessing these objects after postback, and they seem empty. This article describes how to use ViewState to recreate and reinsert the postback values into the objects to access their values.
You have to recreate the controls in each Post Back.
 
Share this answer
 
Comments
VC.J 7-Oct-14 4:13am    
Tadit, is it possible to retain the value in these control
Yes, if you add the controls in Post Back, the values in the controls can be accessed through code.
Yash khurana 7-Oct-14 6:43am    
Tadit can you tell me how to store the value of panel in ViewState in which dynamically controls are added to recreate after the postback.....I am confused in it.....
Thanks
Panel is already there in aspx. You just need to make sure that the controls you are adding inside it are recreated on every PostBack. I hope if you just remove the !IsPostBack check in Page Load Event, all will be good.
Yash khurana 7-Oct-14 7:20am    
I have also tried this but when i try to add the values in drop down list which is dynamically created shows error....
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900