GridView 自定义分页
<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
}
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="AccessDataSource1" Width="750px">
<Columns>
<asp:TemplateField HeaderText="大类" SortExpression="firstclass_id">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("firstclass_id") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Convert.ToInt32(Eval("firstclass_id"))==1?"法律法规":(Convert.ToInt32(Eval("firstclass_id"))==2?"办事指南":(Convert.ToInt32(Eval("firstclass_id"))==3?"工作流程":"收费情况")) %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="70px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="小类" SortExpression="secondclass_id">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("secondclass_id") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderStyle Width="100px" />
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Convert.ToInt32(Eval("firstclass_id"))==1?Convert.ToInt32(Eval("secondclass_id"))==1?"法律":(Convert.ToInt32(Eval("secondclass_id"))==2?"法规":(Convert.ToInt32(Eval("secondclass_id"))==3?"规章":(Convert.ToInt32(Eval("secondclass_id"))==4?"规范性文件":"已废止政策法规文件"))):(Convert.ToInt32(Eval("firstclass_id"))==2?(Convert.ToInt32(Eval("secondclass_id"))==1?"地籍管理":(Convert.ToInt32(Eval("secondclass_id"))==2?"矿产管理":(Convert.ToInt32(Eval("secondclass_id"))==3?"测绘管理":"建设用地"))):(Convert.ToInt32(Eval("firstclass_id"))==3?(Convert.ToInt32(Eval("secondclass_id"))==1?"规划管理":(Convert.ToInt32(Eval("secondclass_id"))==2?"整理复垦":(Convert.ToInt32(Eval("secondclass_id"))==3?"土地利用":(Convert.ToInt32(Eval("secondclass_id"))==4?"测绘管理":(Convert.ToInt32(Eval("secondclass_id"))==5?"地质矿产":(Convert.ToInt32(Eval("secondclass_id"))==6?"地籍管理":(Convert.ToInt32(Eval("secondclass_id"))==7?"执法监察":"综合业务"))))))):"收费标准")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="doctitle" HeaderText="文档标题" SortExpression="doctitle">
<HeaderStyle Width="300px" />
</asp:BoundField>
<asp:BoundField DataField="filepath" HeaderText="文档路径" SortExpression="filepath">
<HeaderStyle Width="350px" />
</asp:BoundField>
<asp:CommandField ShowDeleteButton="True">
<HeaderStyle Width="30px" />
</asp:CommandField>
</Columns>
<HeaderStyle HorizontalAlign="Left" />
<PagerTemplate>
<asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label> of <asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page" Enabled ="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page" Enabled="<%# ((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>
</PagerTemplate>
</asp:GridView>