一(yī)直以來都覺得分(fēn)頁是麻煩的事情,.NET中(zhōng)雖然有DATAGRID的分(fēn)頁,但用其他形式的列表仍然需要使用到分(fēn)頁,一(yī)次一(yī)次的寫不利于效率及面向對象的方法,用類或用戶控件也總覺得怪怪的,用第3方的自己覺得不放(fàng)心,也不利于自己進行修改,幹脆就自己寫了一(yī)個。
(另外(wài)注意:在控件編譯時,可以在AssemblyInfo.cs文件中(zhōng)設置控件的标簽和名稱空間,如:
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Web.UI;
[assembly: TagPrefix("PublicControls","PublicControls")] // 自定義控件前綴
第一(yī)個參數是名稱空間(必須是你的控件類的名稱空間),第二個是标簽名(可自定義)
記得要加入System.Web.UI;名稱空間,另外(wài) 将控件類内的
[DefaultProperty("Text"),
ToolboxData("<PublicControls:PageNavigation runat=server></PublicControls:PageNavigation>")]這句屏蔽掉
所有代碼如下(xià):
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
namespace PublicControls
{
///<summary>
/// PageNavigation 分(fēn)頁導航控件。
///</summary>
[DefaultProperty("Text"),
ToolboxData("<PublicControls:PageNavigation runat=server></PublicControls:PageNavigation>")]
public class PageNavigation : Control,IPostBackDataHandler,IPostBackEventHandler
{
#region預定義
private string _style;
private int _count;
private int _pagesize;
private int _pageindex;
private int _pages; //頁群
private int _currentpages;
private string _first;
private string _pres;
private string _pre;
private string _next;
private string _nexts;
private string _last;
///<summary>
///委托頁面索引事件
///</summary>
public event EventHandler PageIndexChange;
#endregion
#region屬性
///<summary>
///相關屬性樣式字符串屬性
///</summary>
[Bindable(false),
Category("StyleString"),
DefaultValue("Align=left")]
public string StyleString
{
get
{
_style = (string)ViewState["StyleString"];
return _style;
}
set
{
_style= value;
ViewState["StyleString"] = _style;
}
}
///<summary>
///記錄總數
///</summary>
[Bindable(false),
Category("Count"),
DefaultValue(0)]
public int Count
{
get
{
if (this.IsNumeric(ViewState["Count"]))
{
_count = (int)ViewState["Count"];
}
return _count;
}
set
{
_count = value;
ViewState["Count"] = _count;
}
}
///<summary>
///頁面大(dà)小(xiǎo)
///</summary>
[Bindable(false),
Category("Pagesize"),
DefaultValue(10)]
public int Pagesize
{
get
{
if (this.IsNumeric(ViewState["Pagesize"]))
{
_pagesize = (int)ViewState["Pagesize"];
}
return _pagesize;
}
set
{
_pagesize = value;
ViewState["Pagesize"] = _pagesize;
}
}
///<summary>
///當前頁索引
///</summary>
[Bindable(false),
Category("Pageindex"),
DefaultValue(0)]
public int Pageindex
{
get
{
if (this.IsNumeric(ViewState["Pageindex"]))
{
_pageindex = (int)ViewState["Pageindex"];
}
return _pageindex;
}
set
{
_pageindex = value;
ViewState["Pageindex"] = _pageindex;
}
}
///<summary>
///頁群屬性
///</summary>
public int Pages
{
get
{
if (this.IsNumeric(ViewState["Pages"]))
{
_pages = (int)ViewState["Pages"];
}
return _pages;
}
set
{
_pages = value;
ViewState["Pages"] = _pages;
}
}
///<summary>
///當前頁群索引
///</summary>
public int CurrentPages
{
get
{
if (this.IsNumeric(ViewState["CurrentPages"]))
{
_currentpages = (int)ViewState["CurrentPages"];
}
return _currentpages;
}
set
{
_currentpages = value;
ViewState["CurrentPages"] = _currentpages;
}
}
///<summary>
///标記樣式
///</summary>
[Bindable(false),
Category("first"),
DefaultValue("第一(yī)頁")]
public string First
{
get
{
_first = (string)ViewState["First"];
return _first;
}
set
{
_first = value;
ViewState["First"] = _first;
}
}
///<summary>
///标記樣式
///</summary>
[Bindable(false),
Category("pres"),
DefaultValue("前N頁")]
public string Pres
{
get
{
_pres = (string)ViewState["Pres"];
return _pres;
}
set
{
_pres = value;
ViewState["Pres"] = _pres;
}
}
///<summary>
///标記樣式
///</summary>
[Bindable(false),
Category("pre"),
DefaultValue("前一(yī)頁")]
public string Pre
{
get
{
_pre = (string)ViewState["Pre"];
return _pre;
}
set
{
_pre = value;
ViewState["Pre"] = _pre;
}
}
///<summary>
///标記樣式
///</summary>
[Bindable(false),
Category("next"),
DefaultValue("後一(yī)頁")]
public string Next
{
get
{
_next = (string)ViewState["Next"];
return _next;
}
set
{
_next = value;
ViewState["Next"] = _next;
}
}
///<summary>
///标記樣式
///</summary>
[Bindable(false),
Category("nexts"),
DefaultValue("後N頁")]
public string Nexts
{
get
{
_nexts = (string)ViewState["Nexts"];
return _nexts;
}
set
{
_nexts = value;
ViewState["Nexts"] = _nexts;
}
}
///<summary>
///标記樣式
///</summary>
[Bindable(false),
Category("last"),
DefaultValue("最後一(yī)頁")]
public string Last
{
get
{
_last = (string)ViewState["Last"];
return _last;
}
set
{
_last = value;
ViewState["Last"] = _last;
}
}
#endregion
#region事件
///<summary>
///當由類實現時,使服務器控件能夠處理将窗體(tǐ)發送到服務器時引發的事件。
///</summary>
///<param name="e">所傳遞的參數</param>
public void RaisePostBackEvent(string e)
{
//當發生(shēng)回送的時候改變控件當前索引
if(e=="top")
{
Pageindex = 0;
CurrentPages = 0 ;
}
else if(e=="last")
{
Pageindex = (Count+Pagesize-1)/Pagesize -1;
CurrentPages = ((Count+Pagesize-1)/Pagesize+Pages-1)/ Pages -1;
if(Pageindex<0)
{
Pageindex=0;
}
if(CurrentPages<0)
{
CurrentPages=0;
}
}
else if(e=="pre")
{
if(Pageindex>0)
{
Pageindex--;
if(Pageindex<CurrentPages*Pages && Pageindex>0)
{
CurrentPages--;
}
}
}
else if(e=="next")
{
if(Pageindex<(Count+Pagesize-1)/Pagesize -1)
{
Pageindex++;
if(Pageindex>(CurrentPages+1)*Pages-1 && Pageindex<Count-1)
{
CurrentPages++;
}
}
}
else if(e=="pres")
{
Pageindex -= Pages;
if(Pageindex<0)
{
Pageindex = 0;
}
if(Pageindex<CurrentPages*Pages && Pageindex>0)
{
CurrentPages--;
}
}
else if(e=="nexts")
{
Pageindex += Pages;
if(Pageindex>(Count+Pagesize-1)/Pagesize -1)
{
Pageindex = (Count+Pagesize-1)/Pagesize -1;
}
if(Pageindex>(CurrentPages+1)*Pages-1 && Pageindex<Count-1)
{
CurrentPages++;
}
}
else
{
Pageindex = int.Parse(e.ToString());
}
//發生(shēng)回送事件時引發OnPageIndexChange事件
OnPageIndexChange(System.EventArgs.Empty);
}
///<summary>
///當由類實現時,爲 ASP.NET 服務器控件處理回發數據。
///</summary>
///<param name="postDataKey">數據集合元素索引</param>
///<param name="values">string 的排序集合</param>
///<returns></returns>
public bool LoadPostData(string postDataKey, NameValueCollection values)
{
//Pageindex = Int32.Parse(values[this.UniqueID].Split('|')[0]);
//CurrentPages = Int32.Parse(values[this.UniqueID].Split('|')[1]);
return false;
}
///<summary>
///當由類實現時,用信号要求服務器控件對象通知(zhī) ASP.NET 應用程序該控件的狀态已更改。
///</summary>
public void RaisePostDataChangedEvent()
{
}
///<summary>
///當頁面索引改變,觸發委托
///</summary>
///<param name="e"></param>
protected void OnPageIndexChange(System.EventArgs e)
{
//委托給加載控件頁的PageIndexChange事件
if (PageIndexChange!=null)
{
PageIndexChange(this,e);
}
}
///<summary>
///預呈現
///</summary>
///<param name="e"></param>
protected override void OnPreRender(EventArgs e)
{
}
#endregion
#region輸出
///<summary>
///将此控件呈現給指定的輸出參數。
///</summary>
///<param name="output">要寫出到的 HTML 編寫器 </param>
protected override void Render(HtmlTextWriter output)
{
string Pagestring = ""; //定義頁碼呈現字符串
string Text = null; //輸出主結構字符串變量定義
int NO;
//輸出主結構
Text = "<div $style$>";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"top")+ "\">"+First+"</a> ";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"pres")+ "\">"+Pres+"</a> ";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"pre")+ "\">"+Pre+"</a> ";
Text +="$Pagestring$";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"next")+ "\">"+Next+"</a> ";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"nexts")+ "\">"+Nexts+"</a> ";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"last")+ "\">"+Last+"</a> ";
Text +="</div>";//<input type = hidden name ="+this.UniqueID+" value = "+this.Pageindex+"|"+this.CurrentPages+">";
//當頁大(dà)小(xiǎo)小(xiǎo)于0時,還原爲1,防止預算出現除0錯誤
if (!(Pagesize>0))
{
Pagesize = 1;
}
//計算出總頁數,并循環輸出頁碼
for (int i = 0;i< Pages ;i++ )
{
//獲得頁群内頁碼
NO = Pages * CurrentPages + i;
if(NO<(Count+Pagesize-1)/Pagesize)
{
//判斷頁碼是否爲當前頁
if (Pageindex != NO)
{
Pagestring += "<a href = \"javascript:"+ Page.GetPostBackEventReference(this,NO.ToString())+ "\">"+(NO+1).ToString()+"</a> ";
}
//如果不是,頁面部分(fēn)無連接
else
{
Pagestring += (NO+1).ToString()+" ";
}
}
}
if (!(Pagestring.Trim().Length>0))
{
Pagestring = "1";
}
//替換對齊屬性
Text = Text.Replace("$style$",StyleString);
//替換頁碼部分(fēn)
Text = Text.Replace("$Pagestring$",Pagestring);
output.Write(Text);
}
#endregion
#region其他函數
///<summary>
///判斷是否爲數字字符串
///</summary>
///<param name="str">需驗證的字符串</param>
///<returns>判斷結果,符合條件爲True,不符合條件爲False</returns>
public bool IsNumeric(string str)
{
//判斷是否爲空
if (str == null || str.Length==0)
{
return false;
}
//循環檢查每個字符
foreach(char c in str)
{
if (!Char.IsNumber(c))
{
return false;
}
}
return true;
}
///<summary>
///判斷是否爲數字字符串
///</summary>
///<param name="obj">需驗證的字符串</param>
///<returns>判斷結果,符合條件爲True,不符合條件爲False</returns>
public bool IsNumeric(object obj)
{
obj += "";
string str = obj.ToString();
//判斷是否爲空
if (str == null || str.Length==0)
{
return false;
}
//循環檢查每個字符
foreach(char c in str)
{
if (!Char.IsNumber(c))
{
return false;
}
}
return true;
}
#endregion
}
}
(另外(wài)注意:在控件編譯時,可以在AssemblyInfo.cs文件中(zhōng)設置控件的标簽和名稱空間,如:
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Web.UI;
[assembly: TagPrefix("PublicControls","PublicControls")] // 自定義控件前綴
第一(yī)個參數是名稱空間(必須是你的控件類的名稱空間),第二個是标簽名(可自定義)
記得要加入System.Web.UI;名稱空間,另外(wài) 将控件類内的
[DefaultProperty("Text"),
ToolboxData("<PublicControls:PageNavigation runat=server></PublicControls:PageNavigation>")]這句屏蔽掉
所有代碼如下(xià):
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
namespace PublicControls
{
///<summary>
/// PageNavigation 分(fēn)頁導航控件。
///</summary>
[DefaultProperty("Text"),
ToolboxData("<PublicControls:PageNavigation runat=server></PublicControls:PageNavigation>")]
public class PageNavigation : Control,IPostBackDataHandler,IPostBackEventHandler
{
#region預定義
private string _style;
private int _count;
private int _pagesize;
private int _pageindex;
private int _pages; //頁群
private int _currentpages;
private string _first;
private string _pres;
private string _pre;
private string _next;
private string _nexts;
private string _last;
///<summary>
///委托頁面索引事件
///</summary>
public event EventHandler PageIndexChange;
#endregion
#region屬性
///<summary>
///相關屬性樣式字符串屬性
///</summary>
[Bindable(false),
Category("StyleString"),
DefaultValue("Align=left")]
public string StyleString
{
get
{
_style = (string)ViewState["StyleString"];
return _style;
}
set
{
_style= value;
ViewState["StyleString"] = _style;
}
}
///<summary>
///記錄總數
///</summary>
[Bindable(false),
Category("Count"),
DefaultValue(0)]
public int Count
{
get
{
if (this.IsNumeric(ViewState["Count"]))
{
_count = (int)ViewState["Count"];
}
return _count;
}
set
{
_count = value;
ViewState["Count"] = _count;
}
}
///<summary>
///頁面大(dà)小(xiǎo)
///</summary>
[Bindable(false),
Category("Pagesize"),
DefaultValue(10)]
public int Pagesize
{
get
{
if (this.IsNumeric(ViewState["Pagesize"]))
{
_pagesize = (int)ViewState["Pagesize"];
}
return _pagesize;
}
set
{
_pagesize = value;
ViewState["Pagesize"] = _pagesize;
}
}
///<summary>
///當前頁索引
///</summary>
[Bindable(false),
Category("Pageindex"),
DefaultValue(0)]
public int Pageindex
{
get
{
if (this.IsNumeric(ViewState["Pageindex"]))
{
_pageindex = (int)ViewState["Pageindex"];
}
return _pageindex;
}
set
{
_pageindex = value;
ViewState["Pageindex"] = _pageindex;
}
}
///<summary>
///頁群屬性
///</summary>
public int Pages
{
get
{
if (this.IsNumeric(ViewState["Pages"]))
{
_pages = (int)ViewState["Pages"];
}
return _pages;
}
set
{
_pages = value;
ViewState["Pages"] = _pages;
}
}
///<summary>
///當前頁群索引
///</summary>
public int CurrentPages
{
get
{
if (this.IsNumeric(ViewState["CurrentPages"]))
{
_currentpages = (int)ViewState["CurrentPages"];
}
return _currentpages;
}
set
{
_currentpages = value;
ViewState["CurrentPages"] = _currentpages;
}
}
///<summary>
///标記樣式
///</summary>
[Bindable(false),
Category("first"),
DefaultValue("第一(yī)頁")]
public string First
{
get
{
_first = (string)ViewState["First"];
return _first;
}
set
{
_first = value;
ViewState["First"] = _first;
}
}
///<summary>
///标記樣式
///</summary>
[Bindable(false),
Category("pres"),
DefaultValue("前N頁")]
public string Pres
{
get
{
_pres = (string)ViewState["Pres"];
return _pres;
}
set
{
_pres = value;
ViewState["Pres"] = _pres;
}
}
///<summary>
///标記樣式
///</summary>
[Bindable(false),
Category("pre"),
DefaultValue("前一(yī)頁")]
public string Pre
{
get
{
_pre = (string)ViewState["Pre"];
return _pre;
}
set
{
_pre = value;
ViewState["Pre"] = _pre;
}
}
///<summary>
///标記樣式
///</summary>
[Bindable(false),
Category("next"),
DefaultValue("後一(yī)頁")]
public string Next
{
get
{
_next = (string)ViewState["Next"];
return _next;
}
set
{
_next = value;
ViewState["Next"] = _next;
}
}
///<summary>
///标記樣式
///</summary>
[Bindable(false),
Category("nexts"),
DefaultValue("後N頁")]
public string Nexts
{
get
{
_nexts = (string)ViewState["Nexts"];
return _nexts;
}
set
{
_nexts = value;
ViewState["Nexts"] = _nexts;
}
}
///<summary>
///标記樣式
///</summary>
[Bindable(false),
Category("last"),
DefaultValue("最後一(yī)頁")]
public string Last
{
get
{
_last = (string)ViewState["Last"];
return _last;
}
set
{
_last = value;
ViewState["Last"] = _last;
}
}
#endregion
#region事件
///<summary>
///當由類實現時,使服務器控件能夠處理将窗體(tǐ)發送到服務器時引發的事件。
///</summary>
///<param name="e">所傳遞的參數</param>
public void RaisePostBackEvent(string e)
{
//當發生(shēng)回送的時候改變控件當前索引
if(e=="top")
{
Pageindex = 0;
CurrentPages = 0 ;
}
else if(e=="last")
{
Pageindex = (Count+Pagesize-1)/Pagesize -1;
CurrentPages = ((Count+Pagesize-1)/Pagesize+Pages-1)/ Pages -1;
if(Pageindex<0)
{
Pageindex=0;
}
if(CurrentPages<0)
{
CurrentPages=0;
}
}
else if(e=="pre")
{
if(Pageindex>0)
{
Pageindex--;
if(Pageindex<CurrentPages*Pages && Pageindex>0)
{
CurrentPages--;
}
}
}
else if(e=="next")
{
if(Pageindex<(Count+Pagesize-1)/Pagesize -1)
{
Pageindex++;
if(Pageindex>(CurrentPages+1)*Pages-1 && Pageindex<Count-1)
{
CurrentPages++;
}
}
}
else if(e=="pres")
{
Pageindex -= Pages;
if(Pageindex<0)
{
Pageindex = 0;
}
if(Pageindex<CurrentPages*Pages && Pageindex>0)
{
CurrentPages--;
}
}
else if(e=="nexts")
{
Pageindex += Pages;
if(Pageindex>(Count+Pagesize-1)/Pagesize -1)
{
Pageindex = (Count+Pagesize-1)/Pagesize -1;
}
if(Pageindex>(CurrentPages+1)*Pages-1 && Pageindex<Count-1)
{
CurrentPages++;
}
}
else
{
Pageindex = int.Parse(e.ToString());
}
//發生(shēng)回送事件時引發OnPageIndexChange事件
OnPageIndexChange(System.EventArgs.Empty);
}
///<summary>
///當由類實現時,爲 ASP.NET 服務器控件處理回發數據。
///</summary>
///<param name="postDataKey">數據集合元素索引</param>
///<param name="values">string 的排序集合</param>
///<returns></returns>
public bool LoadPostData(string postDataKey, NameValueCollection values)
{
//Pageindex = Int32.Parse(values[this.UniqueID].Split('|')[0]);
//CurrentPages = Int32.Parse(values[this.UniqueID].Split('|')[1]);
return false;
}
///<summary>
///當由類實現時,用信号要求服務器控件對象通知(zhī) ASP.NET 應用程序該控件的狀态已更改。
///</summary>
public void RaisePostDataChangedEvent()
{
}
///<summary>
///當頁面索引改變,觸發委托
///</summary>
///<param name="e"></param>
protected void OnPageIndexChange(System.EventArgs e)
{
//委托給加載控件頁的PageIndexChange事件
if (PageIndexChange!=null)
{
PageIndexChange(this,e);
}
}
///<summary>
///預呈現
///</summary>
///<param name="e"></param>
protected override void OnPreRender(EventArgs e)
{
}
#endregion
#region輸出
///<summary>
///将此控件呈現給指定的輸出參數。
///</summary>
///<param name="output">要寫出到的 HTML 編寫器 </param>
protected override void Render(HtmlTextWriter output)
{
string Pagestring = ""; //定義頁碼呈現字符串
string Text = null; //輸出主結構字符串變量定義
int NO;
//輸出主結構
Text = "<div $style$>";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"top")+ "\">"+First+"</a> ";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"pres")+ "\">"+Pres+"</a> ";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"pre")+ "\">"+Pre+"</a> ";
Text +="$Pagestring$";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"next")+ "\">"+Next+"</a> ";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"nexts")+ "\">"+Nexts+"</a> ";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"last")+ "\">"+Last+"</a> ";
Text +="</div>";//<input type = hidden name ="+this.UniqueID+" value = "+this.Pageindex+"|"+this.CurrentPages+">";
//當頁大(dà)小(xiǎo)小(xiǎo)于0時,還原爲1,防止預算出現除0錯誤
if (!(Pagesize>0))
{
Pagesize = 1;
}
//計算出總頁數,并循環輸出頁碼
for (int i = 0;i< Pages ;i++ )
{
//獲得頁群内頁碼
NO = Pages * CurrentPages + i;
if(NO<(Count+Pagesize-1)/Pagesize)
{
//判斷頁碼是否爲當前頁
if (Pageindex != NO)
{
Pagestring += "<a href = \"javascript:"+ Page.GetPostBackEventReference(this,NO.ToString())+ "\">"+(NO+1).ToString()+"</a> ";
}
//如果不是,頁面部分(fēn)無連接
else
{
Pagestring += (NO+1).ToString()+" ";
}
}
}
if (!(Pagestring.Trim().Length>0))
{
Pagestring = "1";
}
//替換對齊屬性
Text = Text.Replace("$style$",StyleString);
//替換頁碼部分(fēn)
Text = Text.Replace("$Pagestring$",Pagestring);
output.Write(Text);
}
#endregion
#region其他函數
///<summary>
///判斷是否爲數字字符串
///</summary>
///<param name="str">需驗證的字符串</param>
///<returns>判斷結果,符合條件爲True,不符合條件爲False</returns>
public bool IsNumeric(string str)
{
//判斷是否爲空
if (str == null || str.Length==0)
{
return false;
}
//循環檢查每個字符
foreach(char c in str)
{
if (!Char.IsNumber(c))
{
return false;
}
}
return true;
}
///<summary>
///判斷是否爲數字字符串
///</summary>
///<param name="obj">需驗證的字符串</param>
///<returns>判斷結果,符合條件爲True,不符合條件爲False</returns>
public bool IsNumeric(object obj)
{
obj += "";
string str = obj.ToString();
//判斷是否爲空
if (str == null || str.Length==0)
{
return false;
}
//循環檢查每個字符
foreach(char c in str)
{
if (!Char.IsNumber(c))
{
return false;
}
}
return true;
}
#endregion
}
}