title: Programmatically add UserControl with events
body: <p>I need to add multiple user controls to a panel for further editing of the contained data. My user control contains some panels, dropdown lists and input elements, which are populated in the user control's Page_Load event.</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
    // populate comparer ddl from enum
    string[] enumNames = Enum.GetNames(typeof (SearchComparision));
  
    var al = new ArrayList();
    for (int i = 0; i &lt; enumNames.Length; i++)
      al.Add(new {Value = i, Name = enumNames[i]});
  
    scOperatorSelection.DataValueField = "Value";
    scOperatorSelection.DataTextField = "Name";
    ...
  </code></pre>
  
  <p>The data to be displayed is added to the user control as a Field, defined above Page_Load. The signature of the events is the following:</p>
  
  <pre><code>public delegate void ControlStateChanged(object sender, SearchCriteriaEventArgs eventArgs);
  
  public event ControlStateChanged ItemUpdated;
  public event ControlStateChanged ItemRemoved;
  public event ControlStateChanged ItemAdded;
  </code></pre>
  
  <p>The update button on the user control triggers the following method:</p>
  
  <pre><code>protected void UpdateCriteria(object sender, EventArgs e)
  {
    var searchCritCtl = (SearchCriteria) sender;
  
    var scEArgs = new SearchCriteriaEventArgs
    {
      TargetCriteria = searchCritCtl.CurrentCriteria.CriteriaId,
      SearchComparision = ParseCurrentComparer(searchCritCtl.scOperatorSelection.SelectedValue),
      SearchField = searchCritCtl.scFieldSelection.SelectedValue,
      SearchValue = searchCritCtl.scFilterValue.Text,
      ClickTarget = SearchCriteriaClickTarget.Update
    };
    if (ItemUpdated != null)
      ItemUpdated(this, scEArgs);
  }
  </code></pre>
  
  <p>The rendering page fetches the data objects from a storage backend and displays it in it's Page_Load event. This is the point where it starts getting tricky: i connect to the custom events!</p>
  
  <pre><code>int idIt = 0;
  foreach (var item in _currentSearch.Items)
  {
    SearchCriteria sc = (SearchCriteria)LoadControl("~/content/controls/SearchCriteria.ascx");
    sc.ID = "scDispCtl_" + idIt;
    sc.ControlMode = SearchCriteriaMode.Display;
    sc.CurrentCriteria = item;
    sc.ItemUpdated += CriteriaUpdated;
    sc.ItemRemoved += CriteriaRemoved;
    pnlDisplayCrit.Controls.Add(sc);
    idIt++;
  }
  </code></pre>
  
  <p>When first rendering the page, everything is displayed fine, i get all my data. When i trigger an update event, the user control event is fired correctly, but all fields and controls of the user control are <strong>NULL</strong>. After a bit of research, i had to come to the conclusion that the event is fired before the controls are initialized...</p>
  
  <p>Is there any way to prevent such behavior / to override the page lifecycle somehow? I cannot initialize the user controls in the page's Init-event, because i have to access the Session-Store (not initialized in Page_Init).</p>
  
  <p>Any advice is welcome...</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Since we hold all criteria informations in the storage backend (including the count of criteria) and that store uses the userid from the session, we cannot use Page_Init... just for clarification</p>
  
  <p><strong>EDIT #2:</strong></p>
  
  <p>I managed to get past some of the problems. Since i'm now using simple types, im able to bind all the data declaratively (using a repeater with a simple ItemTemplate). It is bound to the control, they are rendered in correct fashion. On Postback, all the data is rebound to the user control, data is available in the OnDataBinding and OnLoad events, everything looks fine.</p>
  
  <p>But as soon it enters the real event (bound to the button control of the user control), all field values are lost somehow...</p>
  
  <p>Does anybody know, how the page lifecycle continues to process the request after Databinding/Loading ? I'm going crazy about this issue...</p>