AspNetPager 와 GridView 를 사용 하여 페이지 를 완성 합 니 다.
18931 단어 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;
}
[출처블 로그 가든,전재 작가 출처 를 밝 혀 주세요]
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C#의 413은 DataGridView에서 Excel로 데이터 내보내기텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.