title: "Failed to load view state" Error When dynamically loading user control
body: <p>I am having a User control [ascx] which is a radgrid. It also has a Edit column template which allow user to Insert/ change/update the values.</p>
  
  <p>I am loading this user control on click of a button [Say EDIT] in aspx page. </p>
  
  <p>When I click on <code>EDIT</code> button of aspx page the user control loads perfectly, but when I click on the <code>Add New Record</code> of user control I get this error message.</p>
  
  <blockquote>
    <p>Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save
    viewstate during the previous request. For example, when adding
    controls dynamically, the controls added during a post-back must match
    the type and position of the controls added during the initial
    request.</p>
  </blockquote>
  
  <p>//code of user control -ascx </p>
  
  <pre><code>&lt;telerik:RadGrid AllowAutomaticDeletes="True"
      AllowAutomaticInserts="False" AllowAutomaticUpdates="False"
      Height="410px" ID="rgrd1" runat="server" AutoGenerateColumns="False"
      OnUpdateCommand="rgrd1_UpdateCommand"
      OnInsertCommand="rgrd1_InsertCommand"  OnNeedDataSource="rgrd1_NeedDataSource" GridLines="None"&gt;
  
      &lt;MasterTableView CommandItemDisplay="top" CommandItemStyle-Wrap="False" CommandItemStyle-HorizontalAlign="Left"
          DataKeyNames="Type"&gt;
          &lt;Columns&gt;
              &lt;telerik:GridEditCommandColumn UniqueName="EditColumn" HeaderStyle-HorizontalAlign="left"
                  HeaderText="Edit" ButtonType="ImageButton"&gt;
              &lt;/telerik:GridEditCommandColumn&gt;
              &lt;telerik:GridTemplateColumn DataField="Type" HeaderStyle-HorizontalAlign="left"
                  HeaderText="Type" UniqueName="Type"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="TypeLabel" runat="server" Text='&lt;%# Eval("Type") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/telerik:GridTemplateColumn&gt;
  
          &lt;/Columns&gt;
          &lt;EditFormSettings EditFormType="Template"&gt;
              &lt;EditColumn FilterControlAltText="Filter EditCommandColumn1 column" UniqueName="EditCommandColumn1"&gt;
              &lt;/EditColumn&gt;
              &lt;FormTemplate&gt;
                  &lt;table&gt;
                          &lt;td&gt;
                              &lt;telerik:RadTextBox ID="txtType" Width="50" runat="server"  Text='&lt;%# Eval("Type") %&gt;'&gt;
                              &lt;/telerik:RadTextBox&gt;
  
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/table&gt;
              &lt;/FormTemplate&gt;
          &lt;/EditFormSettings&gt;
  
      &lt;/MasterTableView&gt;
      &lt;HeaderStyle HorizontalAlign="Center" /&gt;
  &lt;/telerik:RadGrid&gt;
  </code></pre>
  
  <p>.....................................
  Code of my aspx page - in which i am loading the above usercontrol in Radwindow</p>
  
  <p>On click of EDIT button - below code i have written- </p>
  
  <pre><code>RadWindow window = new RadWindow();
  window.Height = Unit.Pixel(500);
  window.Width = Unit.Pixel(500);
  window.VisibleOnPageLoad = true;
  UserControl uc = (UserControl)Page.LoadControl("../../Controls/TypeUserControl.ascx");
  uc.EnableViewState = false;
  window.ContentContainer.Controls.Add(uc);
  pnl.Controls.Add(window);
  </code></pre>
  
  <p>When I click on <code>Add New Record</code> in usercontrol it throws the exception "Failed to load view state:"</p>
  
  <p>I have aspx page on which this user control is loaded is being inherited by my BasePage.</p>
  
  <p>Exception is coming in OnPreRender of base page</p>
  
  <pre><code>protected override void OnPreRender(EventArgs e)
  {
      try
      {
           base.OnPreRender(e);
      }
  }
  </code></pre>
  
  <p>User control - code behing </p>
  
  <pre><code>    public partial class TypeUserControl : System.Web.UI.UserControl
  {
  
      protected void rgrd1Types_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
      {
          try
          {
             //dbase call
          }
          catch (Exception ex)
          {
              SetValidationMessage(ex.Message.ToString());
          }
      }
  
      protected void rgrd1_InsertCommand(object source, GridCommandEventArgs e)
      {
          try
          {
              //dbase call
  
          }
          catch (Exception ex)
          {
              SetValidationMessage(ex.Message.ToString());
          }
      }
  </code></pre>