<PagerTemplate>
<table>
<tr>
<td>
<asp:Label ID="LabelCurrentPage" runat="server"
>当前页:<%# ((GridView)Container.NamingContainer).PageIndex + 1 %></asp:Label></td>
<td>
<asp:Label ID="LabelPageCount" runat="server" >总页数:<%# ((GridView)Container.NamingContainer).PageCount %></asp:Label></td>
<td>
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
Enable="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton></td>
<td>
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"
Enable="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton></td>
<td>
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
Enable="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton></td>
<td>
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
Enable="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</asp:LinkButton></td>
</tr>
</table>
</PagerTemplate>
把以上表格放到<PagerTemplate>即可
在
ASP.NET 2.0
种提供了
GridView
控件。该控件的分页比较方便,可以通过在
Visual Studio .NET 2005
种简单设置即可实现各种分页功能。
1.
默认分页方式
(1)
是否允许分页
GridView
的
AllowPaging
属性。
(2)
每页记录数
GridView
的
PageSize
(3)
分页导航条形式
GridView
的
PagerSettings
属性的
Mode
:
Numeric
,
NextPrevious
,
NextPreviousFirstLast
,
NumericFirstLast
。
2.
自定义分页
(
1)
当前页
<
asp:Label
ID
="LabelCurrentPage"
runat
="server"
Text
="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></
asp:Label
>
(2)
总页数
<
asp:Label
ID
="LabelPageCount"
runat
="server"
Text
="<%# ((GridView)Container.NamingContainer).PageCount %>"></
asp:Label
>
(3)
首页、上一页、下一页、尾页
<
asp:LinkButton
ID
="LinkButtonFirstPage"
runat
="server"
CommandArgument
="First"
CommandName
="Page"
Visible
="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">
首页
</
asp:LinkButton
>
![]()
<
asp:LinkButton
ID
="LinkButtonPreviousPage"
runat
="server"
CommandArgument
="Prev"
CommandName
="Page"
Visible
="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">
上一页
</
asp:LinkButton
>
![]()
<
asp:LinkButton
ID
="LinkButtonNextPage"
runat
="server"
CommandArgument
="Next"
CommandName
="Page"
Visible
="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">
下一页
</
asp:LinkButton
>
![]()
<
asp:LinkButton
ID
="LinkButtonLastPage"
runat
="server"
CommandArgument
="Last"
CommandName
="Page"
Visible
="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">
尾页
</
asp:LinkButton
>
<asp:textbox id="txtNewPageIndex" runat="server" width="20px" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />
<asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="GO" />
注
:
将上述代码放在
GridView
的
<PagerTemplate></PagerTemplate>
PageIndexChanging
事件中加入如下代码
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView theGrid = sender as GridView; // refer to the GridView
int newPageIndex = 0;
if (-2 == e.NewPageIndex) { // when click the "GO" Button
TextBox txtNewPageIndex = null;
//GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
GridViewRow
pagerRow = theGrid.BottomPagerRow; //GridView
较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
//updated at 2006
年月日:15:33
if (null != pagerRow) {
txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox; // refer to the TextBox with the NewPageIndex value
}
if (null != txtNewPageIndex) {
newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
}
}
else { // when click the first, last, previous and next Button
newPageIndex = e.NewPageIndex;
}
// check to prevent form the NewPageIndex out of the range
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
// specify the NewPageIndex
theGrid.PageIndex = newPageIndex;
// rebind the control
// in this case of retrieving the data using the xxxDataSoucr control,
// just do nothing, because the asp.net engine binds the data automatically
}