title: Getting Failed to load viewstate error
body: <p>First off, I am not dynamically creating any controls.  This is the order I take to produce the error:</p>
  
  <p>I have a listview on the page, when I click the edit link under the listview, I display a panel which is hidden by default.  The panel has a few buttons on it along with some listboxes.  When I click an item in the listbox or click one of the buttons, I get the following error:</p>
  
  <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>
  
  <p>Again, I am not creating anything dynamically, I am just hiding the panel with the controls by default and then displaying them, so I am not sure why I am getting this error.</p>
  
  <p>Here is some code:</p>
  
  <p>PAGE LOAD</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if (!Page.IsPostBack)
      {
          Session["Albums"] = null;
          Albums = AlbumCollection.GetAlbums(Common.GetUserName(), 
                                                         ddlAlbumType.SelectedIndex);
  
          lvwAlbums.DataSource = Albums;
          lvwAlbums.DataBind();
       }
  }
  </code></pre>
  
  <p>When I click the edit link, this is the code that runs:</p>
  
  <pre><code>protected void lvwAlbums_RowEditing(object sender, ListViewEditEventArgs e)
  {
      this.AlbumId = int.Parse(         
      this.lvwAlbums.DataKeys[e.NewEditIndex].Values["AlbumId"].ToString());
  
      this.AlbumName=
      this.lvwAlbums.DataKeys[e.NewEditIndex].Values["AlbumName"].ToString();
  
      Album album = new Album(this.AlbumId);
  
      ViewState["AlbumId"] = this.AlbumId;
      ViewState["AlbumName"] = this.AlbumName;
  
      pnlAlbum.Visible = true; // This panel holds the controls
      btnEditAlbum.Visible = true;
      btnCancel.Visible = true;
      EditAlbum(this.AlbumId);
  
          this.lvwAlbums.EditIndex = e.NewEditIndex;
          AlbumCollection.GetAlbums(Common.GetUserName(),ddlAlbumType.SelectedIndex);
      }
  </code></pre>
  
  <p>If I click the cancel button, I get the error, but it also happens if click another button on the panel such as Add/Remove... Here is the code for the Cancel button:</p>
  
  <pre><code>pnlAlbum.Visible = false;
  this.lvwAlbums.EditIndex = -1;
  AlbumCollection.GetAlbums(Common.GetUserName(), ddlAlbumType.SelectedIndex);
  </code></pre>
  
  <p>Here is the aspx/html for the ListView:</p>
  
  <pre><code>&lt;asp:ListView ID="lvwAlbums" 
                runat="server" 
                GroupItemCount="5" 
                DataKeyNames="AlbumId,AlbumName"
                OnItemEditing="lvwAlbums_RowEditing" 
                OnItemCommand="lvwAlbums_ItemCommand"
                OnItemDeleting="lvwAlbums_RowDeleting"  
                OnSelectedIndexChanging="lvwAlbums_SelectedIndexChanging"
                OnPagePropertiesChanging="lvwAlbums_PagePropertiesChanging"&gt;
   &lt;EditItemTemplate&gt;
   &lt;td&gt;
   &lt;div&gt;
   &lt;asp:TextBox ID="txtAlbumName" runat="server"
                Text='&lt;%# Eval("AlbumName").ToString().Trim() %&gt;' /&gt;
  
   &lt;asp:LinkButton ID="lnkView" runat="server" Text="View" CommandName="View"
                   CommandArgument='&lt;%# Eval("AlbumId") %&gt;'&gt;
   &lt;/asp:LinkButton&gt;
   |
   &lt;asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit"
                   CommandArgument='&lt;%# Eval("AlbumId") %&gt;'&gt;
   &lt;/asp:LinkButton&gt;
   |
   &lt;asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete"
                   CommandArgument='&lt;%# Eval("AlbumId") %&gt;'&gt;
   &lt;/asp:LinkButton&gt;
   &lt;br /&gt;
   &lt;span&gt;Songs:
   &lt;%# Eval("total") %&gt;&lt;/span&gt;
   &lt;/div&gt;
   &lt;/td&gt;
   &lt;/EditItemTemplate&gt;
   &lt;LayoutTemplate&gt;
   &lt;asp:DataPager runat="server" ID="ItemDataPager" PageSize="20" 
                  PagedControlID="lvwAlbums"&gt;
   &lt;Fields&gt;
   &lt;asp:NumericPagerField ButtonType="Link" NumericButtonCssClass="pager" /&gt;
   &lt;/Fields&gt;
   &lt;/asp:DataPager&gt;
   &lt;table&gt;
   &lt;tr&gt;
   &lt;td&gt;
   &lt;table&gt;
   &lt;asp:PlaceHolder runat="server" ID="groupPlaceHolder"&gt;&lt;/asp:PlaceHolder&gt;
   &lt;/table&gt;
   &lt;/td&gt;
   &lt;/tr&gt;
   &lt;/table&gt;
   &lt;/LayoutTemplate&gt;
   &lt;GroupTemplate&gt;
   &lt;tr&gt;
   &lt;asp:PlaceHolder runat="server" ID="itemPlaceHolder"&gt;&lt;/asp:PlaceHolder&gt;
   &lt;/tr&gt;
   &lt;/GroupTemplate&gt;
   &lt;ItemTemplate&gt;
   &lt;td&gt;
   &lt;asp:Literal ID="litAlbumName" runat="server" 
                Text='&lt;%#  Eval("AlbumName").ToString().Trim() %&gt;' /&gt;
   &lt;br /&gt;
   &lt;asp:LinkButton ID="lnkView" runat="server" Text="View" CommandName="View"
                   CommandArgument='&lt;%# Eval("AlbumId") %&gt;'&gt;
   &lt;/asp:LinkButton&gt;
   |
   &lt;asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit"
                   CommandArgument='&lt;%# Eval("AlbumId") %&gt;'&gt;
   &lt;/asp:LinkButton&gt;
   |
   &lt;asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete"
                   CommandArgument='&lt;%# Eval("AlbumId") %&gt;'&gt;
   &lt;/asp:LinkButton&gt;
   &lt;br /&gt;
   &lt;span&gt;Songs:
   &lt;%# Eval("total") %&gt;&lt;/span&gt;
   &lt;/td&gt;
   &lt;/ItemTemplate&gt;
   &lt;/asp:ListView&gt;
  </code></pre>
  
  <p>Here is the markup for the Panel:</p>
  
  <pre><code>&lt;asp:Panel ID="pnlAlbum" runat="server" Visible="false"&gt;
  &lt;asp:ListBox ID="lstAvailableSongs" runat="server" SelectionMode="Multiple"&gt;   
  &lt;/asp:ListBox&gt;
  &lt;asp:Button ID="btnAddAll" runat="server" Text="Add All" OnClick="btnAddAll_Click" /&gt;
  &lt;asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /&gt;
  &lt;asp:Button ID="btnRemove" runat="server" Text="Remove" OnClick="btnRemove_Click" /&gt;
  &lt;asp:Button ID="btnRemoveAll" runat="server" 
              Text="Remove All"OnClick="btnRemoveAll_Click" /&gt;
  &lt;asp:ListBox ID="lstSelectedSongs" runat="server" SelectionMode="Multiple"&gt;  
  &lt;/asp:ListBox&gt;
  &lt;asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" /&gt;
  &lt;asp:Button ID="btnEditAlbum" runat="server"Text="Save"
              ValidationGroup="CreateAlbum" OnClick="btnEditAlbum_Click" /&gt;
  &lt;asp:Button ID="btnSaveAs" runat="server" Text="Save As" ValidationGroup="CreateAlbum"
              OnClick="btnSaveAs_Click" /&gt;
  &lt;/asp:Panel&gt;
  </code></pre>
  
  <p>Here is some extra info:</p>
  
  <p>I put an update panel around one of the listboxes in the panel and when I clicked the edit link under a listview item, I received the following error:</p>
  
  <p>Microsoft JScript runtime error: Sys.InvalidOperationException: Could not find UpdatePanel with ID 'ctl00_ctl00_InnerContent_MainContent_UpdatePanel4'. If it is being updated dynamically then it must be inside another UpdatePanel.</p>
  
  <p>Putting an UpdatePanel around the whole asp.net panel resolved the issue above, but I still get the Failed to load viewstate error when clicking on Cancel or Add, etc...</p>