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.Collections.Generic; /// <summary>
/// GridViewPager 的摘要說明 /// </summary> public class GridViewPager<T> { public GridViewPager() { // // TODO: 在此處添加構造函數(shù)邏輯 // } public static string GetPageNum(IList<T> list, GridView GridViewName, int PageSize)
{ int Page = 0, AllPage = 0, Next = 0, Pre = 0, StartCount = 0, EndCount = 0; string PageStr = "", QueryStr = ""; string[] Temp_Arr = null; string FilePath = HttpContext.Current.Request.CurrentExecutionFilePath; string currentPath = HttpContext.Current.Request.Url.Query; //string SelPage = " 轉到<select name=SelPage id=SelPage onchange=\"javascript:location.href=this.value\">"; int startIndex = currentPath.IndexOf("&"); int startIndex2 = currentPath.IndexOf("="); if (startIndex < 0 && startIndex2 < 0) { QueryStr = "?"; } if (startIndex < 0) { //QueryStr = "?"; if (startIndex2 > 0) { Temp_Arr = currentPath.Split('='); if (Temp_Arr[0] != "?Page") { QueryStr = ""; QueryStr = Temp_Arr[0] + "=" + Temp_Arr[1] + "&"; } else { QueryStr = "?"; } }
} else { string Temp = null; string[] Params_Array = null; string[] nameValues = currentPath.Split('&'); QueryStr = ""; Temp = ""; foreach (string param in nameValues) { if (param.IndexOf("=") > 0) { Params_Array = param.Split('='); if (Params_Array[0] != "Page") { Temp += Params_Array[0] + "=" + Params_Array[1] + "&"; } } } QueryStr = Temp; } PagedDataSource ObjPds = new PagedDataSource();
ObjPds.DataSource = list; ObjPds.AllowPaging = true; int Total = list.Count; ObjPds.PageSize = PageSize; if (HttpContext.Current.Request.QueryString["Page"] != null)
{ Page = Convert.ToInt32(HttpContext.Current.Request.QueryString["Page"]); } else { Page = 1; } ObjPds.CurrentPageIndex = Page - 1; GridViewName.DataSource = ObjPds; GridViewName.DataBind(); if (Page < 1) Page = 1; if (PageSize != 0) { AllPage = (Total / PageSize); AllPage = ((Total % PageSize) != 0 ? AllPage + 1 : AllPage); AllPage = (AllPage == 0 ? 1 : AllPage); } Next = Page + 1; Pre = Page - 1; StartCount = (Page + 5) > AllPage ? AllPage - 9 : Page - 4; //中間頁起始序號 EndCount = Page < 5 ? 10 : Page + 5; //中間頁終止序號 if (StartCount < 1) { StartCount = 1; }//為了避免輸出的時候產生負數(shù),設置如果小于1就從序號1開始 if (AllPage < EndCount) { EndCount = AllPage; }//頁碼+5的可能性就會產生最終輸出序號大于總頁碼,那么就要將其控制在頁碼數(shù)之內 PageStr = "共" + AllPage + "頁 ";
PageStr += Page > 1 ? "<a href=\"" + FilePath + QueryStr + "Page=1\">首頁</a> <a href=\"" + FilePath + QueryStr + "Page=" + Pre + "\">上一頁</a>" : "首頁 上一頁"; for (int xk = StartCount; xk < EndCount; xk++) { PageStr += Page == xk ? " <font color=\"#ff0000\">" + xk + "</font>" : " <a href=\"" + FilePath + QueryStr + "Page=" + xk + "\">" + xk + "</a>";
}
PageStr += Page != AllPage ? " <a href=\"" + FilePath + QueryStr + "Page=" + Next + "\">下一頁</a> <a href=\"" + FilePath + QueryStr + "Page=" + AllPage + "\">末頁</a>" : " 下一頁 末頁 ";
return PageStr;
} } |
|
來自: 二寶么么噠 > 《asp.net(C#)》