title: Asp.net C# listview paging Failed to load viewstate
body: <p>I have some problem with paging using Datapager on a list view. 
  The page is very simple. Enter a search text. Click on the search button. Display the result to a list view. The result is display fine but every time i click on the page number i got the following error: </p>
  
  <pre><code>Server Error in '/' Application.
  
  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.
  
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  
  Exception Details: System.Web.HttpException: 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.
  
  Source Error: 
  
  An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
  
  Stack Trace: 
  
  
  [HttpException (0x80004005): 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.]
     System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +317
     System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +144
     System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +204
     System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +144
     System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +204
     System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +144
     System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +204
     System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +144
     System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +204
     System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +144
     System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +204
     System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +144
     System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +204
     System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +144
     System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +204
     System.Web.UI.Page.LoadAllState() +464
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1849
  
  Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34212 
  </code></pre>
  
  <hr>
  
  <p>Here is my makeup:</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Search.aspx.cs" Inherits="ITDB.Views.Employee.Search"%&gt;
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="Header" runat="server"&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt;
      &lt;br /&gt;
      &lt;asp:TextBox ID="SearchText" runat="server"&gt;&lt;/asp:TextBox&gt;
      &lt;asp:Button ID="cmdSearch" runat="server" Text="Search" OnClick="cmdSearch_Click" /&gt;
      &lt;hr /&gt;
  
      &lt;asp:ListView id="SearchList" runat="server"
              DataKeyNames="EmployeeID" 
              ItemType="ITDB.DBContext.Employee"      
              &gt;
              &lt;EmptyDataTemplate&gt;
                  There are no entries found for Employee
              &lt;/EmptyDataTemplate&gt;
              &lt;LayoutTemplate&gt;                                                       
                  &lt;asp:PlaceHolder runat="server" id="itemPlaceholder" /&gt;
                 &lt;asp:DataPager PageSize="5"  runat="server"&gt;
                      &lt;Fields&gt;
                          &lt;asp:NextPreviousPagerField ShowLastPageButton="False" ShowNextPageButton="False" ButtonType="Button" ButtonCssClass="btn" /&gt;
                          &lt;asp:NumericPagerField ButtonType="Button"  NumericButtonCssClass="btn" CurrentPageLabelCssClass="btn disabled" NextPreviousButtonCssClass="btn" /&gt;
                          &lt;asp:NextPreviousPagerField ShowFirstPageButton="False" ShowPreviousPageButton="False" ButtonType="Button" ButtonCssClass="btn" /&gt;
                      &lt;/Fields&gt;
                  &lt;/asp:DataPager&gt;                
              &lt;/LayoutTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;%#: Item.FirstName + " " + Item.LastName%&gt;
                  &lt;br /&gt;   
              &lt;/ItemTemplate&gt;
      &lt;/asp:ListView&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID="Content3" ContentPlaceHolderID="AfterForm" runat="server"&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <hr>
  
  <p>Code behind:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data.Entity;
  using ITDB.DBContext;
  
  namespace ITDB.Views.Employee
  {
      public partial class Search : System.Web.UI.Page
      {
          protected ITDB.DBContext.ITDB_MSSQL_Connection _db = new ITDB.DBContext.ITDB_MSSQL_Connection();
  
          protected void Page_Load(object sender, EventArgs e)
          {
  
          }
  
          protected void cmdSearch_Click(object sender, EventArgs e)
          {
              String szSearch = SearchText.Text;
              SearchList.DataSource = GetSearchList(szSearch);
              SearchList.DataBind();
          }
  
          private List&lt;ITDB.DBContext.Employee&gt; GetSearchList(String searchString)
          {
              return (from employee in _db.Employee
                      where employee.FirstName.Contains(searchString) ||
                            employee.LastName.Contains(searchString)
                      orderby employee.LastName
                      select employee
                          ).ToList();
          }
  
      }
  }
  </code></pre>
  
  <p>What am i doing wrong here?
  Thanks a lot for any help.</p>