用最少的代码给GridView控件分页
<asp:GridView ID="GV_FrientLink" runat="server" AllowPaging="True" Width="100%"
CellPadding="1" AutoGenerateColumns="False" PageSize="5"
onpageindexchanging="GV_FrientLink_PageIndexChanging" DataKeyNames="ID"
onrowcommand="GV_FrientLink_RowCommand" >
<Columns>
<asp:BoundField HeaderText="编号" DataField="ID">
<FooterStyle Width="30px" />
</asp:BoundField>
<asp:BoundField HeaderText="标题" DataField="Title" />
<asp:BoundField HeaderText="链接地址" DataField="Title1" />
<asp:BoundField HeaderText="发布者" DataField="Author" />
<asp:BoundField HeaderText="发布时间" DataField="DateAndTime" />
<asp:HyperLinkField DataNavigateUrlFields="ID,RootCatalogID" DataNavigateUrlFormatString="Modify.aspx?ID={0}&RootCatalogID={1}" HeaderText="编辑" Text="编辑" />
<asp:HyperLinkField DataNavigateUrlFields="ID,RootCatalogID" DataNavigateUrlFormatString="Delete.aspx?ID={0}&RootCatalogID={1}" HeaderText="删除" Text="<div onclick="return confirm('是否删除')">删除</div>" />
</Columns>
<PagerTemplate>
<br />
<asp:Label ID="lblPage" runat="server" Text='<%# "第" + (((GridView)Container.NamingContainer).PageIndex + 1) + "页/共" + (((GridView)Container.NamingContainer).PageCount) + "页" %> '></asp:Label>
<asp:LinkButton ID="lbnFirst" runat="Server" Text="首页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="First" ></asp:LinkButton>
<asp:LinkButton ID="lbnPrev" runat="server" Text="上一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="Prev" ></asp:LinkButton>
<asp:LinkButton ID="lbnNext" runat="Server" Text="下一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Next" ></asp:LinkButton>
<asp:LinkButton ID="lbnLast" runat="Server" Text="尾页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Last" ></asp:LinkButton>
到第<asp:TextBox runat="server" ID="inPageNum" Width="25px"></asp:TextBox>页 <asp:Button ID="GotoPage" CommandName="go" runat="server" Class="Btn" Text="转到"/><br />
</PagerTemplate>
<EmptyDataTemplate>
<asp:Label ID="EmptyMessage" runat="server" Text="未找到符合此条件的数据!请重试!"></asp:Label>
</EmptyDataTemplate>
<PagerStyle CssClass="GridViewPagerStyle" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle CssClass="GridViewHeaderStyle" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
后台:
private void Bind() { DataTable dt = new DataTable(); BLL.Content bll = new BLL.Content(); dt = bll.List(602); this.GV_FrientLink.DataSource = dt; this.GV_FrientLink.DataBind(); } protected void GV_FrientLink_PageIndexChanging(object sender, GridViewPageEventArgs e) { this.GV_FrientLink.PageIndex = e.NewPageIndex; this.Bind(); } //删除跟转到某一页 protected void GV_FrientLink_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "go")//转页 { try { TextBox tb = (TextBox)GV_FrientLink.BottomPagerRow.FindControl("inPageNum"); int num = Int32.Parse(tb.Text); if (num <= 0) { Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('页码必须大于零!!');</script>"); return; } GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1); GV_FrientLink_PageIndexChanging(null, ea); } catch { } } }