title: Dynamically loaded checkbox values lost after dropdownlist changed selection
body: <p>A table is is build up with data that is based on the selected value of a dropdownlist.
  This table contains a checkbox for each row (checked by default).
  On a button postback, I only want to get the rows for which the checkbox is checked.</p>
  
  <p>On the initial load and after a button postback, this works as intended.
  But when you do a button postback after the OnSelectedIndexChanged of the dropdownlist, all the checkboxes are checked again.</p>
  
  <p>I known I have to make it work through the page life-cycle, but can't get it to work proparly.</p>
  
  <p>The code that I use is the following:
  DynamicDropDownList.aspx</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicDropDownList.aspx.cs" Inherits="LittleApplication.DynamicDropDownList" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;asp:DropDownList runat="server" ID="ddlControl" OnSelectedIndexChanged="DropDownChanged" AutoPostBack="True"&gt;&lt;/asp:DropDownList&gt;&lt;br/&gt;&lt;br/&gt;
          &lt;asp:Table runat="server" ID="tblControl"&gt;&lt;/asp:Table&gt;&lt;br/&gt;&lt;br/&gt;
          &lt;asp:Button runat="server" ID="btnControl" Text="Save" 
              onclick="btnControl_Click" /&gt;&lt;br/&gt;&lt;br/&gt;
          &lt;asp:Label runat="server" ID="lblControl"&gt;&lt;/asp:Label&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>DropDownList.aspx.cs</p>
  
  <pre><code>using System;
  using System.Web.UI.WebControls;
  
  namespace LittleApplication
  {
      public partial class DynamicDropDownList : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              if (!IsPostBack)
              {
                  LoadDropDownList();
              }
              LoadTable();
          }
  
          private void LoadDropDownList()
          {
              ddlControl.Items.Add(new ListItem { Text = "First", Value = "1" });
              ddlControl.Items.Add(new ListItem { Text = "Second", Value = "2" });
          }
  
          private void LoadTable()
          {
              tblControl.Rows.Clear();
  
              if (ddlControl.SelectedValue == "1")
              {
                  var firstCheck = new CheckBox {Checked = true};
                  var firstCellCheck = new TableCell();
                  firstCellCheck.Controls.Add(firstCheck);
                  var firstCellText = new TableCell();
                  firstCellText.Text = "First One";
                  var firstRow = new TableRow();
                  firstRow.Cells.Add(firstCellCheck);
                  firstRow.Cells.Add(firstCellText);
                  tblControl.Rows.Add(firstRow);
  
                  var secondCheck = new CheckBox {Checked = true};
                  var secondCellCheck = new TableCell();
                  secondCellCheck.Controls.Add(secondCheck);
                  var secondCellText = new TableCell();
                  secondCellText.Text = "First Two";
                  var secondRow = new TableRow();
                  secondRow.Cells.Add(secondCellCheck);
                  secondRow.Cells.Add(secondCellText);
                  tblControl.Rows.Add(secondRow);
              }
              else
              {
                  var firstCheck = new CheckBox { Checked = true };
                  var firstCellCheck = new TableCell();
                  firstCellCheck.Controls.Add(firstCheck);
                  var firstCellText = new TableCell();
                  firstCellText.Text = "Second One";
                  var firstRow = new TableRow();
                  firstRow.Cells.Add(firstCellCheck);
                  firstRow.Cells.Add(firstCellText);
                  tblControl.Rows.Add(firstRow);
  
                  var secondCheck = new CheckBox { Checked = true };
                  var secondCellCheck = new TableCell();
                  secondCellCheck.Controls.Add(secondCheck);
                  var secondCellText = new TableCell();
                  secondCellText.Text = "Second Two";
                  var secondRow = new TableRow();
                  secondRow.Cells.Add(secondCellCheck);
                  secondRow.Cells.Add(secondCellText);
                  tblControl.Rows.Add(secondRow);   
              }
          }
  
          protected void DropDownChanged(object sender, EventArgs e)
          {
              lblControl.Text = String.Empty;
              LoadTable();
          }
  
          protected void btnControl_Click(object sender, EventArgs e)
          {
              lblControl.Text = String.Empty;
              foreach (TableRow row in tblControl.Rows)
              {
                  if (((CheckBox)row.Cells[0].Controls[0]).Checked)
                  {
                      lblControl.Text += row.Cells[1].Text + "&lt;br/&gt;";
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>To see the behaviour that I described:
  On pageload, uncheck 'First One' and click 'Save'.
  The text 'First Two' appears under the 'Save' button (this is correct).
  Change the dropdownlist value to 'Second.
  Uncheck 'Second One' and click 'Save'.
  The text 'Second One    Second Two' is shown, where only 'Second Two' is expected.</p>
  
  <p>Thanks for the advice.</p>
  
  <p><strong>Edit</strong>
  I should add that this example simulates the behaviour of what is happening in the actual application.
  And it might require some more feedback about the actual working.
  The dropdownlist contains multiple values.
  Based on this selection, different data is fetched from database.
  This data is also different in the number of columns that is shown and such.
  The LoadTable will dynamicaly add this different data to the table.
  The only thing they have in common, is the use of checkboxes.
  So a solution should have as minimal changes to the LoadDropDownList and LoadTable functions.</p>