title: Dynamically adding rows to ASP.NET repeater in reaction to event
body: <p>I am trying to create ASP.NET server control (pure code, without ascx template - because control must be completly contained in .dll and it must not rely on external .ascx files), and I have a problem with dynamically adding items to repeater.</p>
  
  <p>I want to add item to repeater in reaction to SelectedIndexChanged event, but when i do second DataBind() in that event, i lose data from ViewModel (for example, textboxes contains default data instead of text entered by user).</p>
  
  <p>Simplified version of my code (in large portion borrowed from MS composite control example - <a href="http://msdn.microsoft.com/en-us/library/3257x3ea%28v=vs.100%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/3257x3ea%28v=vs.100%29.aspx</a>):</p>
  
  <pre><code>[ToolboxData("&lt;{0}:FilterControl runat=server /&gt;")]
  public class FilterControl : CompositeControl, IPostBackDataHandler
  {
      private List&lt;FilteringProperty&gt; elements = new List&lt;FilteringProperty&gt;();
      private DropDownList filteringElementsDropDownList;
      private Repeater usedFiltersRepeater;
  
      [Bindable(true), DefaultValue(null), Description("Active filters")]
      public List&lt;FilteringProperty&gt; UsedElements
      {
          get
          {
              EnsureChildControls();
              if (ViewState["UsedElements"] == null)
              {
                  ViewState["UsedElements"] = new List&lt;FilteringProperty&gt;();
              }
              return (List&lt;FilteringProperty&gt;)ViewState["UsedElements"];
          }
          set
          {
              EnsureChildControls();
              ViewState["UsedElements"] = value;
          }
      }
  
      protected override void RecreateChildControls()
      {
          EnsureChildControls();
      }
  
      protected override void CreateChildControls()
      {
          Controls.Clear();
  
          filteringElementsDropDownList = new DropDownList { AutoPostBack = true };
          usedFiltersRepeater = new Repeater();
  
          foreach (var element in elements)
          {
              filteringElementsDropDownList.Items.Add(new ListItem(element.DisplayName));
          }
  
          filteringElementsDropDownList.SelectedIndexChanged += (sender, e) =&gt;
          {
              string selectedText = filteringElementsDropDownList.SelectedValue;
              FilteringProperty condition = elements.First(x =&gt; x.DisplayName == selectedText);
  
              var toRemove = filteringElementsDropDownList.Items.Cast&lt;ListItem&gt;().FirstOrDefault(x =&gt; x.Text == condition.DisplayName);
              if (toRemove != null)
              {
                  filteringElementsDropDownList.Items.Remove(toRemove);
              }
  
              UsedElements.Add(condition);
              // ======&gt; A &lt;========
          };
  
          usedFiltersRepeater.ItemDataBound += (sender, args) =&gt;
          {
              FilteringProperty dataItem = (FilteringProperty)args.Item.DataItem;
  
              Control template = args.Item.Controls[0];
              TextBox control = (TextBox)template.FindControl("conditionControl");
              control.Text = dataItem.DisplayName;
              // ======&gt; C &lt;========
          };
  
          usedFiltersRepeater.ItemTemplate = // item template
          usedFiltersRepeater.DataSource = UsedElements;
          usedFiltersRepeater.DataBind();
          // ======&gt; B &lt;========
  
          Controls.Add(filteringElementsDropDownList);
          Controls.Add(usedFiltersRepeater);
      }
  }
  </code></pre>
  
  <p>I marked important portions of code with (A), (B) and (C)</p>
  
  <p>The problem is, (A) is executed after DataBinding (B and C), so changes in UsedElements are not visible until next postback.</p>
  
  <p>It is possible to add <code>usedFiltersRepeater.DataBind();</code> after (A), but than all controls are recreated without data from viewstate (i.e empty)</p>
  
  <p>Is there a way to dynamically change repeater after databinding, such that data of contained controls is preserved?</p>
  
  <p>Tl;dr - i have a DropDownList and I want to add editable items to Repeater on SelectedIndexChanged (without losing viewstate).</p>