程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> GridView分頁代碼簡單萬能實用

GridView分頁代碼簡單萬能實用

編輯:ASP.NET基礎
復制代碼 代碼如下:
<asp:GridView ID="GridViewHistory" runat="server" AutoGenerateColumns="False"
CssClass="vip_table" GridLines="None" BorderStyle="None" CellPadding="0"
ShowHeader="False" AllowPaging="true" PageSize="20"
onpageindexchanging="GridViewHistory_PageIndexChanging">
<PagerTemplate>
<asp:LinkButton ID="lb_firstpage" runat="server" onclick="lb_firstpage_Click">首頁</asp:LinkButton>
<asp:LinkButton ID="lb_previouspage" runat="server"
onclick="lb_previouspage_Click">上一頁</asp:LinkButton>
<asp:LinkButton ID="lb_nextpage" runat="server" onclick="lb_nextpage_Click">下一頁</asp:LinkButton>
<asp:LinkButton ID="lb_lastpage" runat="server" onclick="lb_lastpage_Click">尾頁</asp:LinkButton>
第<asp:Label ID="lbl_nowpage" runat="server" Text="<%#GridViewHistory.PageIndex+1 %>" ForeColor="#db530f"></asp:Label>頁/共<asp:Label
ID="lbl_totalpage" runat="server" Text="<%#GridViewHistory.PageCount %>" ForeColor="#db530f"></asp:Label>頁
</PagerTemplate>


後台代碼:
復制代碼 代碼如下:
//分頁
protected void GridViewHistory_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridViewHistory.PageIndex = e.NewPageIndex;
dataBinding();
}
protected void Button_search_Click(object sender, EventArgs e)
{
dataBinding();
}
protected void lb_firstpage_Click(object sender, EventArgs e)
{
this.GridViewHistory.PageIndex = 0;
dataBinding();
}
protected void lb_previouspage_Click(object sender, EventArgs e)
{
if (this.GridViewHistory.PageIndex > 0)
{
this.GridViewHistory.PageIndex--;
dataBinding();
}
}
protected void lb_nextpage_Click(object sender, EventArgs e)
{
if (this.GridViewHistory.PageIndex < this.GridViewHistory.PageCount)
{
this.GridViewHistory.PageIndex++;
dataBinding();
}
}
protected void lb_lastpage_Click(object sender, EventArgs e)
{
this.GridViewHistory.PageIndex = this.GridViewHistory.PageCount;
dataBinding();
}

dataBinding()為GridViewHistory的數據源綁定事件
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved