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 的摘要說(shuō)明
/// </summary>
public class GridViewPager<T>
{
public GridViewPager()
{
//
// TODO: 在此處添加構(gòu)造函數(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 = " 轉(zhuǎn)到<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; //中間頁(yè)起始序號(hào)
EndCount = Page < 5 ? 10 : Page + 5; //中間頁(yè)終止序號(hào)
if (StartCount < 1) { StartCount = 1; }//為了避免輸出的時(shí)候產(chǎn)生負(fù)數(shù),設(shè)置如果小于1就從序號(hào)1開(kāi)始
if (AllPage < EndCount) { EndCount = AllPage; }//頁(yè)碼+5的可能性就會(huì)產(chǎn)生最終輸出序號(hào)大于總頁(yè)碼,那么就要將其控制在頁(yè)碼數(shù)之內(nèi)
PageStr = "共" + AllPage + "頁(yè) ";
PageStr += Page > 1 ? "<a href=\"" + FilePath + QueryStr + "Page=1\">首頁(yè)</a> <a href=\"" + FilePath + QueryStr + "Page=" + Pre + "\">上一頁(yè)</a>" : "首頁(yè) 上一頁(yè)";
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 + "\">下一頁(yè)</a> <a href=\"" + FilePath + QueryStr + "Page=" + AllPage + "\">末頁(yè)</a>" : " 下一頁(yè) 末頁(yè) ";
return PageStr;
}
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。