AspNetPager 와 GridView 를 사용 하여 페이지 를 완성 합 니 다.

18931 단어 GridView
GridView 의 페이지 기능 이 너무 약 하기 때문에 강력 한 AspNetPager 를 페이지 컨트롤 로 사용 해 야 합 니 다.가장 쉬 운 방법 은 GridView 컨트롤 아래 에 AspNetPager 컨트롤 을 놓 는 것 입 니 다.하지만 이렇게 하면 GridView 의 페이지 기능 을 사용 할 수 없 을 것 같 습 니 다.데이터 양 이 많 지 않 은 상황 에서 GridView 의 페이지 를 사용 하 는 것 은 매우 편리 하고 효과 적 입 니 다.또 하나의 문 제 는 페이지 컨트롤 이 GridView 가 만 든 표 아래 에 있 고 GridView 자체 페이지 처럼 표 내부 에 포함 되 어 있 지 않다 는 점 이다.
이상 의 문 제 를 해결 하려 면 AspNetPager 를 GridView 의 페이지 템 플 릿(PagerTemplate)에 넣 을 수 있 습 니 다.다음 코드 는 다음 과 같 습 니 다.

  
    
< asp:GridView ID = " GridView1 " runat = " server " AutoGenerateColumns = " False " AllowPaging = " True " >
< Columns >
< asp:BoundField DataField = " DmId " HeaderText = " " ReadOnly = " True " SortExpression = " DMID " />
……
……
</ Columns >
< PagerStyle HorizontalAlign = " Center " />
< PagerTemplate >
< asp:AspNetPager ID = " AspNetPager1 " runat = " server " ShowBoxThreshold = " 5 " ShowPageIndexBox = " Auto " CenterCurrentPageButton = " True " >
</ asp:AspNetPager >
</ PagerTemplate >
</ asp:GridView >

그러나 이렇게 하면 몇 가지 문 제 를 해결 해 야 한다.
(1)이 GridView 각 페이지 의 줄 수 AspNetPager 는 모른다.해결 방법:AspNetPager 에 속성 페이지 크기 추가="<%\#(GridView)Container.NamingContainer).페이지 크기%>"
(2)이 GridView 가 연 결 된 총 기록 수 AspNetPager 도 모른다.해결 방법:AspNetPager 에 속성 RecordCount 를 추가 합 니 다="<%\#(IList)(((GridView)Container.NamingContainer).Data Source)).count%>"
(3)이 GridView 는 현재 몇 페이지 에 있 는 AspNetPager 도 모른다.이 문제 의 해결 은 앞에서 처럼 간단 하지 않 습 니 다.속성 CurrentPageIndex 를 설정 하 는 방식 으로 AspNetPager 는 전혀 알 아 보지 못 합 니 다!(AspNetPager 의 bug 일 것 입 니 다.)이 문 제 를 해결 하려 면 페이지 를 넘 길 때마다 배경 코드 에서 AspNetPager 에 CurrentPageIndex 속성 을 설정 해 야 합 니 다.
(4)AspNetPager 를 사용 한 후 GridView 는 PageChanging 이 벤트 를 촉발 하지 않 습 니 다.그러나 AspNetPager 의 PageChanging 이 벤트 를 실행 하려 면 페이지 템 플 릿 에 있 는 AspNetPager 컨트롤 에 이벤트 처 리 를 추가 할 수 있 습 니 다:OnPageChanging="AspNetPager 1PageChanging"에 대응 하 는 것 은 페이지 를 나 누 는 배경 코드 입 니 다.

  
    
protected void AspNetPager1_PageChanging( object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
this .GridView1.PageIndex = e.NewPageIndex - 1 ; // ,AspNetPager PageIndex 1 , GridView 0 , 1.
Bind(); // GridView
Wuqi.Webdiyer.AspNetPager pager = this .GridView1.BottomPagerRow.FindControl( " AspNetPager1 " ) as Wuqi.Webdiyer.AspNetPager;
pager.CurrentPageIndex
= e.NewPageIndex; // 3 。
}

OK,위의 네 가지 문제 가 모두 해결 되 었 습 니 다.우리 의 GridView+AspNetPager 의 페이지 가 완성 되 었 습 니 다!또한 AspNetPager 의 스타일 이 예 쁘 지 않다 고 생각 되면 CSS 를 다시 정의 할 수 있 습 니 다.마지막 전체 코드 는:

  
    
< asp:GridView ID = " GridView1 " runat = " server " AutoGenerateColumns = " False " AllowPaging = " True " >
< Columns >
< asp:BoundField DataField = " DmId " HeaderText = " " ReadOnly = " True " SortExpression = " DMID " />
……
……
</ Columns >
< PagerStyle CssClass = " GridViewPager " HorizontalAlign = " Center " />
< PagerTemplate >
< asp:AspNetPager ID = " AspNetPager1 " runat = " server " ShowBoxThreshold = " 5 " ShowPageIndexBox = " Auto "
PageSize
= " <%# ((GridView)Container.NamingContainer).PageSize%> " OnPageChanging = " AspNetPager1_PageChanging "
RecordCount
= " <%#((IList)(((GridView)Container.NamingContainer).DataSource)).Count %> "
CenterCurrentPageButton
= " True " >
</ asp:AspNetPager >
</ PagerTemplate >
</ asp:GridView >
//
protected void AspNetPager1_PageChanging( object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
this .GridView1.PageIndex = e.NewPageIndex - 1 ;
Bind();
Wuqi.Webdiyer.AspNetPager pager
= this .GridView1.BottomPagerRow.FindControl( " AspNetPager1 " ) as Wuqi.Webdiyer.AspNetPager;
pager.CurrentPageIndex
= e.NewPageIndex;
}

[출처블 로그 가든,전재 작가 출처 를 밝 혀 주세요]

좋은 웹페이지 즐겨찾기