title: Repeater and Custom Control - Dynamically adding to the collection and retaining values
body: <p>It has been so long since I've used Web Forms I find myself not remembering most of the perks.</p>
  
  <p>I have a user control that has a button, a repeater and the ItemTemplate property of the repeater is another user control.</p>
  
  <pre><code>&lt;asp:Button runat="server" ID="btnAdd" CssClass="btn" Text="Add" OnClick="btnAdd_Click"/&gt;
  &lt;br/&gt;
  &lt;asp:Repeater runat="server" ID="rptrRequests"&gt;
      &lt;ItemTemplate&gt;
          &lt;uc1:ucRequest ID="ucNewRequest" runat="server" /&gt;
      &lt;/ItemTemplate&gt;
  &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>The idea is that when the user clicks on the Add button a new instance of the ucRequest is added to the collection. The code behind is as follows:</p>
  
  <pre><code>public partial class ucRequests : UserControl
  {
      public List&lt;ucRequest&gt; requests
      {
          get
          {
              return (from RepeaterItem item in rptrRequests.Items 
                      select (ucRequest) (item.Controls[1])
                      ).ToList();
          }
          set
          {
              rptrRequests.DataSource = value;
              rptrRequests.DataBind();
          }
      }
  
      protected void Page_Load(object sender, EventArgs e)
      {
          if (IsPostBack) return;
  
          requests = new List&lt;ucRequest&gt;();
      }
  
      protected void btnAdd_Click(object sender, EventArgs e)
      {
          var reqs = requests;
          reqs.Add(new ucRequest());
          requests = reqs;
      }
  }
  </code></pre>
  
  <p>After much googling I now remember that I should be binding the Repeater in the OnInit method in order for the ViewState to put the captured data of the controls within the ucRequest control on them between post backs but when I try to do that I will always have a single instance of the control on the Repeater since its Items collection is always empty.</p>
  
  <p>How could I manage to do this?</p>
  
  <p>Thanks in advance.</p>