title: How to retain data for dynamically created controls?
body: <p>I have the following situation: </p>
  
  <ul>
  <li>I have 2 drop down lists (DDL) with auto-postback enabled.</li>
  <li>When the user selects something from first DDL, he can choose stuff from 2nd DDL.</li>
  <li>When the user selects something from 2nd DDL, I open database (WHERE clause of SQL query is filled with values from DDLs), and dynamically create several text boxes, labels and buttons which I place inside a placeholder.</li>
  </ul>
  
  <p>The problem is: when I click on some of the dynamically created buttons, nothing happens, and I can't retrieve data from dynamically created boxes. Also, placeholder content is lost (all controls).</p>
  
  <p>So my question is: how do I retain data from dynamically created controls on postback? </p>
  
  <p>I have listed a lot of similar articles on this site, and none solves my problem. Another thing is - I can't use view state nor session.</p>
  
  <p>Code is:</p>
  
  <pre><code>public Button btn1 = new Button();
  public TextBox txtBoxC1 = new TextBox();
  public TextBox txtBoxC2 = new TextBox();
  public TextBox txtBoxC3 = new TextBox();
  ...
  ...
  ...
  protected void ddl2_SelectedIndexChanged(object sender, EventArgs e)
  {
   ...
   ...
   if some conditions are met:
   ...
   ...
   String CS4 = ConfigurationManager.ConnectionStrings["connStringName"].ConnectionString;
   using (SqlConnection conn4 = new SqlConnection(CS4))
   {
    SqlCommand cmd4 = new SqlCommand("Select * from table where column1=" + 
    Convert.ToInt32(ddl2.SelectedValue)+" order by column1", conn4);
    conn4.Open();
    SqlDataReader rdr = cmd4.ExecuteReader();
    while (rdr.Read())
    {
      txtBoxC1.Text = rdr["column1"].ToString(); txtBoxC1.MaxLength = 3; txtBoxC1.Columns = 3; txtBoxC1.ID = ddl2.SelectedValue + "1";
      txtBoxC2.Text = rdr["column2"].ToString(); txtBoxC2.MaxLength = 3; txtBoxC2.Columns = 3; txtBoxC2.ID = ddl2.SelectedValue + "2";
      txtBoxC3.Text = rdr["column3"].ToString(); txtBoxC3.MaxLength = 3; txtBoxC3.Columns = 3; txtBoxC3.ID = ddl2.SelectedValue + "3";
      btn1.Text = "click me"; btn1.ID = ddl1.SelectedValue; btn1.Click += btn1_Click;
      phDynamic.Controls.Add(btn1);
      phDynamic.Controls.Add(txtBoxC1);
      phDynamic.Controls.Add(txtBoxC2);
      phDynamic.Controls.Add(txtBoxC3);
    }
   }
  }
  ...
  ...
  ...
  private void btn1_Click(object sender, EventArgs e)
  {
    Response.Write(txtBoxC1.Text+txtBoxC2.Text+txtBoxC3.Text);
  }
  </code></pre>
  
  <p>So instead of getting back values from my 3 textboxes, I don't get back anything, and I also lose placeholder controls.</p>