title: Dynamically add drop down lists and remember them through postbacks
body: <p>I'm trying to figure out the best way to achieve this:</p>
  
  <p>I need to show a drop down list when the page loads, the default selected value is nothing, or an empty element (such as "-"). When the user selects one value from the list, another drop down list is added below the first one, and so on.</p>
  
  <p>My problem with this is how you make the page to remember the drop down lists created through postbacks and the values they have selected? Do I have to use some kind of array to store them or something?</p>
  
  <p>Thank you very much.</p>
  
  <p>EDIT: I did this example for dynamically add drop down lists and suscribe them to an event handler but the event won't fire. </p>
  
  <p>EDIT 2: Changed the code because I labelled for VB.Net and posted something in C#. I update the code with some trivial improvements, but still won't fire the event :(</p>
  
  <pre><code>    Private myDdlArray As New List(Of DropDownList)
  
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
      If (Session("ddl") Is Nothing) Then
          Session("ddl") = myDdlArray
      End If
  
      If (Session("ddlcount") Is Nothing) Then
          Session("ddlcount") = 0
      End If
  
      myDdlArray = CType(Session("ddl"), List(Of DropDownList))
  
      Dim myDdl As New DropDownList
      myDdl = New DropDownList
      For Each myDdl In myDdlArray
          panel.Controls.Add(myDdl)
          panel.Controls.Add(New LiteralControl("&lt;br/&gt;"))
      Next
  
      Session("ddl") = myDdlArray
  
  End Sub
  
  Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
  
      myDdlArray = CType(Session("ddl"), List(Of DropDownList))
      Dim ddlCount As Integer = CInt(Session("ddlcount")) + 1
  
      Dim newDdl As New DropDownList
      newDdl.ID = String.Format("ddlNPKMaterial{0}", ddlCount)
      newDdl.AutoPostBack = True
  
      AddHandler newDdl.SelectedIndexChanged, AddressOf cbo_SelectedIndexChanged
  
      newDdl.Items.Add("Uno")
      newDdl.Items.Add("Dos")
      newDdl.Items.Add("Tres")
  
      myDdlArray.Add(newDdl)
  
      panel.Controls.Clear()
  
      Dim myDdl As New DropDownList
      myDdl = New DropDownList
      For Each myDdl In myDdlArray
          panel.Controls.Add(myDdl)
          panel.Controls.Add(New LiteralControl("&lt;br/&gt;"))
      Next
  
      Session("ddl") = myDdlArray
      Session("ddlcount") = ddlCount + 1
  
  End Sub
  
  Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
  
      Session("ddl") = Nothing
      Session("ddlcount") = Nothing
  
  End Sub
  
  Protected Sub btnShow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShow.Click
  
      ' Insert brain here
  
  End Sub
  
  Public Sub cbo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
  
      Response.Write(CType(sender, DropDownList).ID)
  
  End Sub
  
  Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  
      If (Session("ddl") Is Nothing) Then
          panel.Controls.Clear()
      End If
  
  End Sub
  </code></pre>
  
  <p>in the aspx I have:</p>
  
  <pre><code>&lt;form id="form1" runat="server"&gt;
  &lt;div&gt;
      &lt;asp:Button ID="btn" runat="server" Text="Add" /&gt;
      &lt;asp:Button runat="server" ID="btnShow" Text="Tell me" /&gt;
      &lt;asp:Button runat="server" ID="btnReset" Text="Reset" /&gt;
      &lt;br /&gt;
      &lt;asp:Panel runat="server" ID="panel"&gt;
          &lt;asp:GridView runat="server" ID="grd"&gt;
              &lt;Columns&gt;
                  &lt;asp:BoundField HeaderText="Id" DataField="Id" /&gt;
              &lt;/Columns&gt;
          &lt;/asp:GridView&gt;
      &lt;/asp:Panel&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>