2014-12-01 24 views
0

我在列表視圖上使用Datapager進行分頁時遇到了一些問題。 該頁面非常簡單。輸入搜索文本。點擊搜索按鈕。將結果顯示到列表視圖。結果顯示正常,但我每次點擊頁碼時間,我得到了以下錯誤:Asp.net C#listview paging加載視圖狀態失敗

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 

這裏是我的化妝:背後

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Search.aspx.cs" Inherits="ITDB.Views.Employee.Search"%> 
<asp:Content ID="Content1" ContentPlaceHolderID="Header" runat="server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <br /> 
    <asp:TextBox ID="SearchText" runat="server"></asp:TextBox> 
    <asp:Button ID="cmdSearch" runat="server" Text="Search" OnClick="cmdSearch_Click" /> 
    <hr /> 

    <asp:ListView id="SearchList" runat="server" 
      DataKeyNames="EmployeeID" 
      ItemType="ITDB.DBContext.Employee"  
      > 
      <EmptyDataTemplate> 
       There are no entries found for Employee 
      </EmptyDataTemplate> 
      <LayoutTemplate>              
       <asp:PlaceHolder runat="server" id="itemPlaceholder" /> 
       <asp:DataPager PageSize="5" runat="server"> 
        <Fields> 
         <asp:NextPreviousPagerField ShowLastPageButton="False" ShowNextPageButton="False" ButtonType="Button" ButtonCssClass="btn" /> 
         <asp:NumericPagerField ButtonType="Button" NumericButtonCssClass="btn" CurrentPageLabelCssClass="btn disabled" NextPreviousButtonCssClass="btn" /> 
         <asp:NextPreviousPagerField ShowFirstPageButton="False" ShowPreviousPageButton="False" ButtonType="Button" ButtonCssClass="btn" /> 
        </Fields> 
       </asp:DataPager>     
      </LayoutTemplate> 
      <ItemTemplate> 
       <%#: Item.FirstName + " " + Item.LastName%> 
       <br /> 
      </ItemTemplate> 
    </asp:ListView> 
</asp:Content> 
<asp:Content ID="Content3" ContentPlaceHolderID="AfterForm" runat="server"> 
</asp:Content> 

代碼:

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<ITDB.DBContext.Employee> GetSearchList(String searchString) 
     { 
      return (from employee in _db.Employee 
        where employee.FirstName.Contains(searchString) || 
          employee.LastName.Contains(searchString) 
        orderby employee.LastName 
        select employee 
         ).ToList(); 
     } 

    } 
} 

我在這裏做錯了什麼? 非常感謝您的幫助。

+0

你創建的OnPreRender DataPager的事件? – Sandeep 2014-12-02 04:11:04

回答

1

一個很好的答案在這裏,因爲直到運行時才知道這些字段。

http://blog.yeshere.org/2011/04/using-datapager-in-listview.html

設置你的DataPager的的ID來:DataPager1

您應該添加和實施的ListView的PagePropertiesChanging事件。事件參數中的PagePropertiesChangingEventArgs將提供所有您需要的分頁屬性(StartRowIndex和MaximumRows),以便您可以將它們提供給DataPager。

如果DataPager的放在裏面的ListView,像你這樣做:

protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e) { 
    ListView lv = sender as ListView; 
    DataPager pager = lv.FindControl("DataPager1") as DataPager; 
    pager.SetPageProperties(e.StartRowIndex, e.MaximumRows, false); 
    BindData(); // set DataSource to ListView and call DataBind() of ListView 
} 

如果外:

protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e) {  
     this.DataPage1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false); 
     BindData(); // set DataSource to ListView and call DataBind() of ListView 
    }