title: How can I explicitly maintain view state for dynamically created user controls?
body: <p>I have a search form that offers a number of different search options, such as 'search on natural name', 'search on legal name', 'search on address' etc. These are defined by static 'factory' methods on a <code>PropertySearchOption</code> class. Each search option specifies a user control containing the search fields required for that optiom, e.g. the natural name option has name and surname fields. The search option used is determined by a <em>RadioButtonList</em> on the main search page, and when the selection changes in this list I dynamically load the required user control.  My problem is that I can't find a way to preserve the view state of the dynamical control and its search fields. Here is the core code I am using:</p>
  
  <pre><code>&lt;asp:RadioButtonList ID="searchTypeOptions" runat="server" AutoPostBack="true" RepeatDirection="Horizontal" RepeatLayout="Flow" OnSelectedIndexChanged="SearchTypeOptionsSelectedIndexChanged"&gt;
      &lt;asp:ListItem Value="NaturalName"&gt;Name (Natural)&lt;/asp:ListItem&gt;
      &lt;asp:ListItem Value="LegalName"&gt;Legal Name (Business)&lt;/asp:ListItem&gt;
      &lt;asp:ListItem Value="ErfDetails"&gt;Erf Details&lt;/asp:ListItem&gt;
      &lt;asp:ListItem Value="PhysicalAddress"&gt;Physical Address&lt;/asp:ListItem&gt;
      &lt;asp:ListItem Value="FarmDetails"&gt;Farm&lt;/asp:ListItem&gt;
  &lt;/asp:RadioButtonList&gt;
  
  protected void Page_Init(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {
          var typeOption = searchTypeOptions.SelectedItem;
          if (typeOption == null)
          {
              searchTypeOptions.SelectedValue = "ErfDetails";
              LoadSearchForm();
          }
      }
  }
  
  protected void SearchTypeOptionsSelectedIndexChanged(object sender, EventArgs e)
  {
      LoadSearchForm();
  }
  
  protected virtual void LoadSearchForm()
  {
      SearchOption = (PropertySearchOption)typeof(PropertySearchOption).GetProperty(searchTypeOptions.SelectedValue, BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
      searchFormPlaceHolder.Controls.Clear();
      var searchForm = LoadControl(SearchOption.FormControlUrl);
      searchFormPlaceHolder.Controls.Add(searchForm);
      searchTypeLabel.Text = SearchOption.Description;
  }
  </code></pre>
  
  <p>I have to set <em>EnableViewState</em> to false on <code>searchFormPlaceHolder</code>, or I get a viewstate exception when I try and change the current option, and like this the dynamic selection of search forms works fine. However, on the button click postback to perform the search, I lose the search form, because it is only created in Page_Init on the first load, not postbacks. If I place <code>LoadSearchForm</code> outside the if block, it fails because on postback, the <code>SelectedValue</code> of the radio button list is empty.</p>
  
  <p>What can I do, or what should I do, to in a scenario like this, besides run away?</p>