2014-12-01 16 views
0

목록보기에서 Datapager를 사용하여 페이징에 문제가 있습니다. 페이지가 매우 간단합니다. 검색 텍스트를 입력하십시오. 검색 버튼을 클릭하십시오. 목록보기에 결과를 표시하십시오. 결과는 디스플레이 괜찮하지만 난 페이지 번호를 클릭 할 때마다 나는 다음과 같은 오류 있어요 : 뒤에Asp.net C# listview paging viewstate를로드하지 못했습니다.

<%@ 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> 

코드 :

여기

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 
내 메이크업입니다
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

당신이있는 DataPager의 OnPreRender 이벤트를 만들었습니다 외부 경우

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 } 

를? – Sandeep

답변

1

필드는 런타임까지 알 수 없으므로 좋은 대답은 여기에 있습니다. 당신은 추가의 ListView의 PagePropertiesChanging 이벤트를 구현해야합니다 DataPager1

:

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

가에있는 DataPager의 ID를 설정합니다. 이벤트 인수의 PagePropertiesChangingEventArgs는 DataPager에 제공 할 수 있도록 필요한 모든 페이징 속성 (StartRowIndex 및 MaximumRows)을 제공합니다.

당신이 이렇게 같이있는 DataPager는 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 
    }