title: Why do my dynamically added controls loose their values after Postback?
body: <p>To ask my question I have created an <code>aspx</code> file containing a <code>Button</code> and a <code>DataList</code> with an <strong>SqlDataSource</strong>:</p>
  
  <pre><code>    &lt;asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /&gt;
  
      &lt;asp:DataList ID="DataList1" runat="server" DataKeyField="a" 
      DataSourceID="SqlDataSource1" &gt;
          &lt;ItemTemplate&gt;
              a:
              &lt;asp:Label ID="aLabel" runat="server" Text='&lt;%# Eval("a") %&gt;' /&gt;
              &lt;br /&gt;
              b:
              &lt;asp:Label ID="bLabel" runat="server" Text='&lt;%# Eval("b") %&gt;' /&gt;
              &lt;br /&gt;
          &lt;/ItemTemplate&gt;
      &lt;/asp:DataList&gt;
  
  &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="&lt;%$ ConnectionStrings:probaConnectionString %&gt;" 
      SelectCommand="SELECT [a], [b] FROM [PROBA_TABLE]"&gt;&lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p>In my code behind I add <code>TextBoxes</code> to the Items of the DataList. I add to every Item a TextBox in the Page_Load, and another TextBox in the Button Click <code>eventhandler</code> as well.</p>
  
  <pre><code>    public partial class _Default : System.Web.UI.Page
      {   
          protected void Page_Load(object sender, EventArgs e)
          {
              if (IsPostBack)
              {
                  foreach (DataListItem item in DataList1.Items)
                  {
                      item.Controls.Add(new TextBox());
                  }
              }
          }
  
          protected void Button1_Click(object sender, EventArgs e)
          {
              foreach (DataListItem item in DataList1.Items)
              {
                  item.Controls.Add(new TextBox());
              }
          }
  
      }
  }
  </code></pre>
  
  <p>This works fine except one thing. When I click the Button, the TextBoxes which were created in the Page_Load keep their Text value, but the TextBoxes which were created in the <code>Button1_Click</code> lose their Text values. My real problem is more complicated than this, but I think solving this would help me a lot.</p>
  
  <p><img src="https://i.stack.imgur.com/i487R.png" alt="My site after postback"></p>