title: Retain controls and properties generated in SelectedIndexChanged
body: <p>Hi first time posting here so sorry if I am a bit ambiguous.</p>
  
  <p>So I am building an ASP.NET web app and I am using a DropDownList to generate a CheckBoxList for the user to check. After they have checked the boxes they wish to they will press a button that will execute a function based on what checkboxes were checked.</p>
  
  <p>My issue comes in when the page posts back after the button press, meaning that the  selection criteria on the checkboxes are lost because I am regenerating the CheckBoxList on the button press.</p>
  
  <p>I have my click event hitting the SelectedIndexChanged event and setting the selected index on the DropDownList so it retains the selected index/value and generates the CheckBoxList again, but how would I go about retaining the state of the checkboxes?</p>
  
  <p>Again, sorry if it is a bit ambiguous or just a silly question overall.
  Thanks!</p>
  
  <p>Edit:</p>
  
  <p>The following is a basic representation of my code and what I'm trying to do:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if(!IsPostBack)
      {
          //Populate dropdown
      }
  }
  
  protected void Dropdown_SelectedIndexChanged(object sender, EventArgs e)
  {
      if(drpDownList.SelectedIndex &gt; 0)
      {
          string strValue = drpDownList.Value;
  
          CheckBoxList chkBoxList = new CheckBoxList();
  
          //Query database for information based on dropdownlist value
          chkBoxList.Items.Add(new ListItem() { Text = strValue, Value = "" /* what was queried */ });
      }
  }
  
  protected void Button_Click(object sender, EventArgs e)
  {
      foreach (Control ctlControl in pnlPanel.Controls)
      {
          if (ctlControl is CheckBoxList)
          {
              CheckBoxList chkBoxList = (CheckBoxList)ctlControl;
  
              foreach (ListItem lisItem in chkBoxList.Items)
              {
                  if (lisItem.Selected == true)
                  {
                      //Do whatever
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>The issue lies in after you select a dropdownlist index, the page generates a checkboxlist for user interaction. Once they are done and click the button to check the properties of the checkboxlist, it fails to as the checkboxlist is dependant on the SelectedIndexChanged method, meaning that any decisions the user made will be wiped and the code will not execute.</p>