Click here to Skip to main content
15,072,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to create panel with label and button (dynamically) on every dropdown selection.
Each time when I select one value in dropdown one panel with label and button should get created and while I select next value in dropdown another panel with label and button should get created. I tried it but when i select next option from dropdown panel for first value is cleared. Please help me.
Posted
Comments
CHill60 27-Feb-15 6:42am
   
Share the code you tried

1 solution

Designer:


C#
<form id="form1" runat="server">
    <div>
    
        <asp:dropdownlist id="DropDownList1" runat="server" autopostback="True" xmlns:asp="#unknown">
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:listitem>a</asp:listitem>
            <asp:listitem>b</asp:listitem>
            <asp:listitem>c</asp:listitem>
            <asp:listitem>d</asp:listitem>
            <asp:listitem>e</asp:listitem>
        </asp:dropdownlist>
        <br />
        <asp:scriptmanager id="ScriptManager1" runat="server" xmlns:asp="#unknown">
        </asp:scriptmanager>
        <asp:updatepanel id="UpdatePanel1" runat="server" xmlns:asp="#unknown">
        <contenttemplate>
        </contenttemplate>
        <triggers>
        <asp:asyncpostbacktrigger controlid="DropDownList1" />
        </triggers>
        </asp:updatepanel>
    
    </div>
    <form></form></form>



CodeBehind:


public partial class _Default : System.Web.UI.Page 
{
    int i;
    protected void Page_Load(object sender, EventArgs e)
    {
            
        if (Page.IsPostBack==false)
	{
		 i = 0;
            ViewState["value"] = 0;
        }
        i = int.Parse(ViewState["value"].ToString());
 
	}

  
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
         for(int j=0;j<=i;j++)
         {
            Panel pnl=new Panel();
            pnl.ID = j.ToString();
           Button btn= new Button();
           btn.ID = "btn" + j.ToString();
            btn.Text = "btn" + j.ToString();
            pnl.BackColor = System.Drawing.Color.LightGreen;
            pnl.Controls.Add(btn);
            UpdatePanel1.ContentTemplateContainer.Controls.Add(pnl);
            }
       
        i = i + 1;
        ViewState["value"] = i.ToString();
    }
    protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
}
   

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