今天查詢相關資料時,看到這個類寫的很好,就轉過來了啊,感謝原創者!
下面直接是類的代碼,一看就明白!
public class Pageing
{
#region 成員
//// <summary>
/// 數據源 Table
/// </summary>
public DataTable dtblTableww = new DataTable();
//// <summary>
/// 每頁記錄數
/// </summary>
public int intPageSizeww = 20;
//// <summary>
/// 需要獲取第幾頁的數據,從 1 開始
/// </summary>
public int intPageNowww = 1;
//// <summary>
/// 首頁 顯示樣式
/// </summary>
public string strFirstww = "";
//// <summary>
/// 上一頁 顯示樣式
/// </summary>
public string strPrevww = "";
//// <summary>
/// 下一頁 顯示樣式
/// </summary>
public string strNextww = "";
//// <summary>
/// 尾頁 顯示樣式
/// </summary>
public string strLastww = "";
//// <summary>
/// 跳轉 的url鏈接
/// </summary>
public string strTurnUrlww = "";
//// <summary>
/// 跳轉的url鏈接的參數前面不要加?或&
/// </summary>
public string strOptionww = "";
private string strCountww = ""; //共N條信息
private string strPageww = ""; //第N頁/共N頁
private string strTurnww; //跳轉控件
#endregion
#region 屬性
private int intRsCounww = 0; //記錄總數
//// <summary>
/// 總記錄數
/// </summary>
public int GetRsCount
{
get
{
return this.intRsCounww;
}
}
private int intPageCountww = 0; //總頁數
//// <summary>
/// 總頁數
/// </summary>
public int GetPageCount
{
get
{
return this.intPageCountww;
}
}
private string strPageShowww = ""; //顯示頁碼的字符串
//// <summary>
/// 顯示頁碼的字符串
/// </summary>
public string GetPageShow
{
get
{
return this.strPageShowww;
}
}
#endregion
#region 初始構造
//// <summary>
/// 初始構造
/// </summary>
public Pageing()
{
//首頁
strFirstww = "";
//上一頁
strPrevww = "";
//下一頁
strNextww = "";
//尾頁
strLastww = "";
//跳轉控件
strTurnww = "";
//顯示頁碼的字符串
strPageShowww = "";
//當前頁
intPageNowww = GetNumeric(GetRequest("Page"));
if (intPageNowww == 0)
{
intPageNowww = 1;
}
}
#endregion
#region 處理分頁及顯示頁碼
//// <summary>
/// 分頁,返回分頁後的數據 Table
/// </summary>
/// <param name="bolCount">是否顯示 共N條信息</param>
/// <param name="bolPage">是否顯示 第N頁/共N頁</param>
/// <param name="bolFirst">是否顯示 首頁</param>
/// <param name="bolLast">是否顯示 尾頁</param>
/// <param name="bolTurn">是否顯示 跳轉控件</param>
/// <param name="IsEnglish">是否 用中文顯示</param>
/// <returns>返回分頁後的數據 Table</returns>
public DataTable BeginPage(bool bolCount, bool bolPage, bool bolFirst, bool bolLast, bool bolTurn, bool IsChinese)
{
//記錄總數
intRsCounww = dtblTableww.Rows.Count;
//總頁數
intPageCountww = (intRsCounww + intPageSizeww - 1) / intPageSizeww;
//超出最大頁碼
if (intPageNowww > intPageCountww)
{
intPageNowww = intPageCountww;
}
//中文分頁
if (IsChinese)
{
//跳轉
strTurnww = "<input value='" + intPageNowww.ToString() + "' id='txtPageGo' name='txtPageGo' type='text' style='width:25px;'><input name='btnGo' type='button' id='btnGo' value='跳轉' onclick=\"javascript:window.location.href='" + strTurnUrlww + "?page=' + document.getElementById('txtPageGo').value + '" + "&" + strOptionww + "'\">";
//共N條信息
strCountww = "共" + intRsCounww.ToString() + "條信息";
//第N頁/共N頁
strPageww = "第" + intPageNowww.ToString() + "頁/共" + intPageCountww.ToString() + "頁";
if (strFirstww == "")
{
strFirstww = "首頁";
strPrevww = "上一頁";
strNextww = "下一頁";
strLastww = "尾頁";
}
}
else//英文文分頁
{
//跳轉
strTurnww = "<input value='" + intPageNowww.ToString() + "' id='txtPageGo' name='txtPageGo' height='20px' type='text' style='width:25px;'><input name='btnGo' type='button' height='20px' id='btnGo' value='Goto' onclick=\"javascript:window.location.href='" + strTurnUrlww + "?page=' + document.getElementById('txtPageGo').value + '" + "&" + strOptionww + "'\">";
//共N條信息
strCountww = "Total " + intRsCounww.ToString() + " Infos";
//第N頁/共N頁
strPageww = " " + intPageNowww.ToString() + "/" + intPageCountww.ToString() + " ";
if (strFirstww == "")
{
strFirstww = " First ";
strPrevww = " Previous ";
strNextww = " Next ";
strLastww = " Last ";
}
}
DataTable MyTable;
int intGetFirstRs = 0;//獲取本頁數據的開始下標
int intGetLastRs = 0;//獲取本頁數據的結束下標
string strFirstww2;
string strPrevww2;
string strNextww2;
string strLastww2;
//沒有記錄
if (intRsCounww <= 0)
{
strPageShowww = strCountww;
MyTable = dtblTableww;
}
//有記錄
else
{
//只有一頁
if (intPageCountww <= 1)
{
strPageShowww = strCountww + " " + strPageww;
MyTable = dtblTableww;
}
//不止一頁
else
{
//第一頁
if (intPageNowww == 1)
{
strFirstww2 = strFirstww;
strPrevww2 = strPrevww;
}
else
{
strFirstww2 = "<a href=\"" + strTurnUrlww + "?page=1" + "&" + strOptionww + "\">" + strFirstww + "</a>";
strPrevww2 = "<a href=\"" + strTurnUrlww + "?page=" + Convert.ToString(intPageNowww - 1) + "&" + strOptionww + "\">" + strPrevww + "</a>";
}
//最後一頁
if (intPageNowww == intPageCountww)
{
strNextww2 = strNextww;
strLastww2 = strLastww;
}
else
{
strNextww2 = "<a href=\"" + strTurnUrlww + "?page=" + Convert.ToString(intPageNowww + 1) + "&" + strOptionww + "\">" + strNextww + "</a>";
strLastww2 = "<a href=\"" + strTurnUrlww + "?page=" + intPageCountww + "&" + strOptionww + "\">" + strLastww + "</a>";
}
//----處理顯示頁碼-----------
if (bolCount == true)//共N條信息
{
strPageShowww = strPageShowww + " " + strCountww;
}
if (bolPage == true)//第N頁/共N頁
{
strPageShowww = strPageShowww + " " + strPageww;
}
if (bolFirst == true) //首頁
{
strPageShowww = strPageShowww + " " + strFirstww2;
}
strPageShowww = strPageShowww + " " + strPrevww2;//上一頁
strPageShowww = strPageShowww + " " + strNextww2;//下一頁
if (bolLast == true)//尾頁
{
strPageShowww = strPageShowww + " " + strLastww2;
}
if (bolTurn == true)//跳轉控件
{
strPageShowww = strPageShowww + " " + strTurnww;
}
//----處理返回數據-----------
intGetFirstRs = (intPageNowww - 1) * intPageSizeww;
intGetLastRs = intPageNowww * intPageSizeww - 1;
//獲取結束數據下標大於總記錄數時
if (intGetLastRs >= intRsCounww)
{
intGetLastRs = intRsCounww - 1;
}
//循環獲取數據
MyTable = dtblTableww.Clone();
for (int I = intGetFirstRs; I <= intGetLastRs; I++)
{
DataRow MyRow = dtblTableww.Rows[I];
MyTable.ImportRow(MyRow);
}
}
}
return MyTable;
}
#endregion
#region 將字符串轉換成整數
//// <summary>
/// 將字符串轉換成整數
/// </summary>
/// <param name="strValue">將字符傳轉換成整數</param>
/// <returns>int</returns>
private int GetNumeric(string strValue)
{
int IReturn;
if (strValue == null)
{
IReturn = 0;
return IReturn;
}
try
{
IReturn = Convert.ToInt32(strValue);
}
catch
{
IReturn = 0;
}
return IReturn;
}
#endregion
#region 獲取Request值
//// <summary>
/// 獲取Request值
/// </summary>
/// <param name="strRequestName">Request名稱</param>
/// <returns>null則爲空</returns>
private string GetRequest(string strRequestName)
{
if (HttpContext.Current.Request["" + strRequestName + ""] != null)
{
return HttpContext.Current.Request["" + strRequestName + ""];
}
else
{
return "";
}
}
#endregion
// #region --------------------------------調用實例--------------------------
////// <summary>
///// 調用實例
///// </summary>
//protected void BindData()
//{
// Student MyStudent = new Student();
// DataTable MyDataTable = MyStudent.GetList("LIST", null);
// Pageing MyPageing = new Pageing();
// MyPageing.dtblTableww = MyDataTable;//數據源
// MyPageing.intPageSizeww = 4;//每頁顯示多少條
// MyPageing.strTurnUrlww = "MyRepeater.ASPx";//跳轉頁面
// MyPageing.strOptionww = "DoWhat=List";//跳轉頁面帶的所有參數
// //獲取當前頁數據
// this.DataList1.DataSource = MyPageing.BeginPage(true, true, true, true, true, false);
// //獲取當前頁分頁顯示
// this.Label1.Text = MyPageing.GetPageShow;
// this.DataList1.DataBind();
//}
//#endregion
#region 簡要說明
//.Net通用分頁(可以選擇頁碼的顯示樣式,且有中英選擇),自己寫的,還不完善,請朋友們多多指點.
//大概思路是:根據傳進來的DataTable,每頁顯示多少條記錄,當前要顯示第幾頁(從Request中獲得),
//來獲取當前頁要顯示的數據(比如DataTable共有123條記錄,每頁顯示10條,第2頁,則取DataTable的Rows10~19),
//再根據其它傳進來的參數設置分頁及顯示的樣式等.
//當然,可能這種方法的效率等某些方面可能不好,只是個例子,可以根據自己的應用修改.
//效果圖:http://images.cnblogs.com/cnblogs_com/84ww/128905/r_page.GIF
#endregion
}
}