GridView分页,可以根据需要进行自定义,可以在PagerTemplate里设置自己想要的样式(这里GridView的ID为gvDetail),例如:
<PagerTemplate>
<div class="InputLable">
<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" CssClass="link01"></asp:LinkButton>
<asp:LinkButton ID="lbnPrev" runat="server" Text="上一頁" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'
CommandName="Page" CommandArgument="Prev" CssClass="link01"></asp:LinkButton>
<asp:LinkButton ID="lbnNext" runat="Server" Text="下一頁" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>'
CommandName="Page" CommandArgument="Next" CssClass="link01"></asp:LinkButton>
<asp:LinkButton ID="lbnLast" runat="Server" Text="尾頁" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>'
CommandName="Page" CommandArgument="Last" CssClass="link01"></asp:LinkButton>
<asp:Label Text="到第" ID="lblTo" runat="server"></asp:Label><asp:TextBox runat="server"
ID="inPageNum" Width="20" onkeydown="return event.keyCode>=48&&event.keyCode<=57||event.keyCode>=96&&event.keyCode<=105||event.keyCode==8||event.keyCode==46"
CssClass="TextBox"></asp:TextBox><asp:Label ID="lblPageSingle" runat="server" Text="頁"></asp:Label>
<asp:Button ID="Button1" Text="Go" CommandName="go" runat="server" />
</div>
</PagerTemplate>
后台需编写对应的方法事件:
protected void gvDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvDetail.PageIndex = e.NewPageIndex;
//BindDetail(); //绑定数据源
TextBox tb = (TextBox)gvDetail.BottomPagerRow.FindControl("inPageNum");
tb.Text = (gvDetail.PageIndex + 1).ToString();
}
/// <summary>
/// 翻页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvDetail_RowCommand(object sender, GridViewCommandEventArgs e)
{
//跳轉頁碼
if (e.CommandName == "go")
{
TextBox tb = (TextBox)gvDetail.BottomPagerRow.FindControl("inPageNum");
int num;
if (Int32.TryParse(tb.Text, out num))
{
if (num > 0)
{
GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
gvDetail_PageIndexChanging(null, ea);
}
}
}
}
protected void gvDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{
//頁碼多語言
if (e.Row.RowType == DataControlRowType.Pager)
{
string lblPage = string.Format(localize.lblPage, gvDetail.PageIndex + 1, gvDetail.PageCount);
((Label)(e.Row.FindControl("lblPage"))).Text = lblPage;
((LinkButton)(e.Row.FindControl("lbnFirst"))).Text = localize.lbnFirst;
((LinkButton)(e.Row.FindControl("lbnPrev"))).Text = localize.lbnPrev;
((LinkButton)(e.Row.FindControl("lbnNext"))).Text = localize.lbnNext;
((LinkButton)(e.Row.FindControl("lbnLast"))).Text = localize.lbnLast;
((Label)(e.Row.FindControl("lblTo"))).Text = localize.lblTo;
((Label)(e.Row.FindControl("lblPageSingle"))).Text = localize.lblPageSingle;
}
}