出自ProgWiki
GridView分頁用版面
<!--第一部份, PagerTemplate 放到 GridView 內 -->
<PagerTemplate>
<div style="text-align: center">
<table>
<tr style="font-size: 14px;">
<td align="center">
<asp:LinkButton ID="lbFirstPage" runat="server" Font-Size="14px" CommandArgument="First" CommandName="Page">第一頁</asp:LinkButton>
<asp:Literal ID="ltlPageLine1" runat="server"> | </asp:Literal>
<asp:LinkButton ID="lbPreviousPage" runat="server" Font-Size="14px" CommandArgument="Prev" CommandName="Page">上一頁</asp:LinkButton> |
<asp:PlaceHolder ID="phInGridView1" runat="server"></asp:PlaceHolder>
<asp:LinkButton ID="lbNextPage" runat="server" Font-Size="14px" CommandArgument="Next" CommandName="Page">下一頁</asp:LinkButton>
<asp:Literal ID="ltlPageLine2" runat="server"> | </asp:Literal>
<asp:LinkButton ID="lbLastPage" runat="server" Font-Size="14px" CommandArgument="Last" CommandName="Page">最後一頁</asp:LinkButton>
</td>
</tr>
</table>
</div>
</PagerTemplate>
<!--第二部份, lbl_TotalNum 與 lbl_TotalPage 放到 GridView 外 -->
</asp:GridView>
<table width="800px">
<tr>
<td align="left">
<asp:Label ID="lbl_TotalNum" runat="server" Text="Label"></asp:Label>
</td>
<td align="right" style="width:auto">
<asp:Label ID="lbl_TotalPage" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
GridView分頁用程式(1)
- GridView1 需使用 DataBound 事件處理函數
GridHelper myGridHelper = new GridHelper();
protected void GridView1_DataBound(object sender, EventArgs e)
{
myGridHelper.DataGridPaging(sender, e);
}
GridView分頁用程式(2)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
/// <summary>
/// GridHelper 的摘要描述
/// </summary>
public class GridHelper
{
public GridHelper()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
public GridView gv = null;
public void DataGridPaging(object sender, EventArgs e)
{
//以下均為 GridView 下方的「自訂分頁」功能的程式碼
gv = (sender as GridView);
int nTotalCount = 0;
if (gv.DataSource != null)
{
nTotalCount = (((sender as GridView).DataSource) as DataTable).Rows.Count;
}
else
{
DataView dv = (DataView)((SqlDataSource)gv.Page.FindControl(gv.DataSourceID)).Select(DataSourceSelectArguments.Empty);
if (dv != null)
nTotalCount = dv.Count;
}
if (nTotalCount > 0) // 若有查到一筆以上的資料
{
// 取得 GridView 底部的分頁橫列
GridViewRow gvrPagerRow = gv.BottomPagerRow;
#region ********** 以下為 LinkButton 群組中,數字頁碼的邏輯判斷與處理 **********
// GridView目前所在的頁碼
int intCurrentPage = gv.PageIndex + 1;
// 動態建立分頁按鈕
int intPageCount = gv.PageCount; // GridView 的總頁數
int intStartPage = Math.Max(intCurrentPage - 4, 1); // intStartPage: LinkButton群組中要顯示的第一個頁碼 (intCurrentPage: 目前所在的頁碼)
int intEndPage = Math.Min(intPageCount, intStartPage + 9); // intEndPage: LinkButton群組中要顯示的最後一個頁碼 (intPageCount: 總頁數)
// 若目前是在第 1 頁,則隱藏由前端加入的「上一頁」LinkButton
if (intCurrentPage == 1)
((LinkButton)gvrPagerRow.Cells[0].FindControl("lbPreviousPage")).Visible = false;
// 若目前是在最後 1 頁,則隱藏由前端加入的「下一頁」LinkButton
if (intCurrentPage == gv.PageCount)
((LinkButton)gvrPagerRow.Cells[0].FindControl("lbNextPage")).Visible = false;
// 若使用者點選的大於第 5 頁(即第 6 頁開始才會顯示由前端加入的「第一頁」LinkButton)
if (intStartPage > 1)
{
((LinkButton)gvrPagerRow.Cells[0].FindControl("lbFirstPage")).Visible = true; // 顯示由前端加入的「第一頁」LinkButton
}
else
{
((LinkButton)gvrPagerRow.Cells[0].FindControl("lbFirstPage")).Visible = false; // 隱藏由前端加入的「第一頁」LinkButton
((Literal)gvrPagerRow.Cells[0].FindControl("ltlPageLine1")).Visible = false; // 隱藏「上一頁」LinkButton 前面的「|」符號
}
// 依 GridVeiw 目前所在頁數,依次產生多個 LinkButton 數字頁碼
for (int j = intStartPage; j <= intEndPage; j++)
{
LinkButton lbPageNo = new LinkButton();
if (j != intCurrentPage) // 若 LinkButton群組中目前頁碼的數字,不為 GridView 目前所在頁數
{
lbPageNo.Text = Convert.ToString(j);
}
else // 若 LinkButton群組中目前頁碼的數字,亦為 GridView 目前所在頁數,就特別突顯處理
{
lbPageNo.Text = Convert.ToString(j);
lbPageNo.Font.Underline = false;
lbPageNo.Font.Size = 11; // 把 LinkButton群組中代表目前頁碼的數字,Font Size 放大一級
lbPageNo.ForeColor = System.Drawing.Color.Tomato;//.OrangeRed;
}
lbPageNo.ID = (lbPageNo + j.ToString());
lbPageNo.CommandArgument = (j - 1).ToString(); // 把 GridView 的目前所在頁數減 4 後,指派給 10 個 LinkButton 去顯示 (10 個頁碼數字依次遞增)
lbPageNo.Click += new EventHandler(lbPageNo_Click);
gvrPagerRow.Cells[0].Controls.Add(lbPageNo);
Literal litBlank = new Literal();
litBlank.Text = " | ";
gvrPagerRow.Cells[0].Controls.Add(litBlank);
((PlaceHolder)gvrPagerRow.Cells[0].FindControl("phInGridView1")).Controls.Add(lbPageNo);
((PlaceHolder)gvrPagerRow.Cells[0].FindControl("phInGridView1")).Controls.Add(litBlank);
}
// 後面的頁數,要比目前所在的頁碼多出 5 筆以上(至少 6 筆),才會顯示由前端加入的「最後一頁」LinkButton
if (intPageCount > intEndPage)
{
((LinkButton)gvrPagerRow.Cells[0].FindControl("lbLastPage")).Visible = true; // 顯示由前端加入的「最後一頁」LinkButton
}
else
{
((LinkButton)gvrPagerRow.Cells[0].FindControl("lbLastPage")).Visible = false; // 隱藏由前端加入的「最後一頁」LinkButton
((Literal)gvrPagerRow.Cells[0].FindControl("ltlPageLine2")).Visible = false; // 隱藏「下一頁」LinkButton 後面的「|」符號
}
#endregion
#region ********* 設定 GridView 下方,左右兩個 Label 要顯示的「共有幾筆」、「第幾頁/共幾頁」**********
((Label)gv.Page.FindControl("lbl_TotalNum")).Text = string.Format("共 {0} 筆",
nTotalCount.ToString("###,###,##0") );
// GridView目前所在的頁碼
int intCurrentPage2 = gv.PageIndex + 1;
// 顯示「目前頁數/總頁數」
((Label)gv.Page.FindControl("lbl_TotalPage")).Text = string.Format("第 {0} 頁 / 共 {1} 頁",
intCurrentPage2.ToString("###,###,##0"),
gv.PageCount.ToString("###,###,##0"));
#endregion
}
else // 若一筆資料都沒查到
{
// GridView 裡的提示訊息,由前端的 EmptyDataTemplate 負責顯示
// 設定 GridView 下方,左右兩個 Label 要顯示的「共有幾筆」、「第幾頁/共幾頁」
((Label)gv.Page.FindControl("lbl_TotalNum")).Text = "共 0 筆";
((Label)gv.Page.FindControl("lbl_TotalPage")).Text = "第 1 頁 / 共 1 頁";
}
}
public void lbPageNo_Click(object sender, EventArgs e)
{
gv.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandArgument);
}
}