ASP.NET分頁組件學與用——教學篇(原始碼)
來源:互聯網
上載者:User
asp.net|分頁|原始碼 using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Text;
namespace NSLzhPages
{
public class LzhPages : System.Web.UI.WebControls.WebControl
{
private int _count;//每頁顯示的記錄條數
private int _currentPage;//當前頁
private int _allCount;//所有記錄條數
private int _showPages;//顯示頁數
//以下指令碼用於從文字框輸入頁碼
private const string SCRIPTSTRING = "<script language='javascript'>\n" +
" function go(ctrl,max)\n" +
" {\n" +
" if(ctrl.value >= 1 && ctrl.value <= max)\n" +
" {\n" +
" var url;\n" +
" var index;\n" +
" url = location.href;\n" +
" index = url.indexOf('?');\n" +
" if(index == -1)\n" +
" {\n" +
" }\n" +
" else\n" +
" {\n" +
" url = url.substring(0,index);\n" +
" }\n" +
" location.href = url + '?currentPage=' + ctrl.value;\n" +
" }\n" +
" else\n" +
" {\n" +
" alert('您輸入的頁碼必須是符合頁面要求的數字,最大值是:' + max);\n" +
" return false;\n" +
" }\n" +
" }\n" +
"</script>\n";
[DefaultValue(10),Category("Customer")]
public int Count
{
set
{
if(value <= 0)
_count = 1;
else
_count = value;
}
get
{
return _count;
}
}
[DefaultValue(1),Category("Customer")]
public int CurrentPage
{
set
{
if(value < 0)
_currentPage = 1;
else
_currentPage = value;
}
get
{
return _currentPage;
}
}
[DefaultValue(1),Category("Customer")]
public int AllCount
{
get
{
return _allCount;
}
set
{
if(_allCount < 0)
throw new Exception("記錄總條數不能為負數");
else
_allCount = value;
}
}
[DefaultValue(1),Category("Customer")]
public int Pages//總頁數
{
get
{
if(this._allCount % this._count > 0)
return ((int)this._allCount / this._count) + 1;
else
return ((int)this._allCount / this._count);
}
}
[DefaultValue(1),Category("Customer")]
public int ShowPages
{
set
{
if(value > 0)
_showPages = value;
else
_showPages = 9;
}
get
{
return _showPages;
}
}
protected override void Render(HtmlTextWriter writer)
{
base.Render (writer);
string leftInfo;
StringBuilder centerInfo = new StringBuilder();
//分頁條分三部分,leftInfo是最左邊的部分,用來顯示當前頁/總頁數,每頁顯示的記錄條數
leftInfo = "頁:" + this.CurrentPage.ToString() + "/" + this.Pages.ToString() + " " + "每頁" + this.Count.ToString() + "條" + " 共" + this.AllCount.ToString() + "條";
//中間的部分是分頁部分
int min;//要顯示的頁面數最小值
int max;//要顯示的頁面數最大值
if(this.CurrentPage > this.Pages)//當前頁必須小於最大頁
{
this.CurrentPage = this.Pages;
}
if(this.CurrentPage % this.ShowPages == 0) //如果恰好整除
{
min = this.CurrentPage + 1;
max = this.CurrentPage + this.ShowPages ;
}
else if(this.CurrentPage % this.ShowPages == 1 && this.CurrentPage > this.ShowPages )
{
min = (((int)this.CurrentPage / this.ShowPages ) - 1) * this.ShowPages +1;
max = this.CurrentPage - 1;
}
else
{
min = ((int)this.CurrentPage / this.ShowPages) * this.ShowPages + 1;
max = (((int)this.CurrentPage / this.ShowPages) +1) * this.ShowPages;
}
string numberStr = " ";//迴圈產生數字序列部分
string AbsUrl;//URL?左邊的部分
AbsUrl = this.Context.Request.Url.ToString();
if(AbsUrl.IndexOf("?") == -1)
{
}
else
{
AbsUrl = AbsUrl.Substring(0,AbsUrl.IndexOf("?"));
}
for(int i = min ; i <= max ; i++)
{
if(i <= this.Pages)//只有不大於最大頁才顯示
{
if(this.CurrentPage == i)//如果是當前頁,用斜體和紅色顯示
{
numberStr = numberStr + "<a href=" + AbsUrl + "?currentPage=" + i.ToString() + ">" + "<I style='color:red'>" + i.ToString() + "</I>" +"</a>" + "\n";
}
else
{
numberStr = numberStr + "<a href=" + AbsUrl + "?currentPage=" + i.ToString() + ">" + i.ToString() +"</a>" + "\n";
}
}
}
//第一頁,上一頁,下一頁,最後一頁
string First,Previous,Next,Last;
First = AbsUrl + "?currentPage=1";
/////////
if(this.CurrentPage == 1)
Previous = AbsUrl + "?currentPage=1";
else
Previous = AbsUrl + "?currentPage=" + (this.CurrentPage - 1).ToString();
/////////
if(this.CurrentPage == this.Pages)
Next = AbsUrl + "?currentPage=" + this.Pages;
else
Next = AbsUrl + "?currentPage=" + (this.CurrentPage + 1).ToString();
/////////
Last = AbsUrl + "?currentPage=" + this.Pages;
centerInfo.AppendFormat("<font face='Webdings' style='font-size:14px'><a href={0}>7</a><a href={1}>3</a></font>{2}<font face='Webdings' style='font-size:14px'><a href={3}>4</a><a href={4}>8</a></font>",First,Previous,numberStr,Next,Last);
StringBuilder sb = new StringBuilder();//HTML字串
sb.AppendFormat("<table style = 'font-size:12px' border='0' cellpadding='0' cellspacing='0' width='100%'> \n " +
"<tr>\n" +
"<td width='25%' align='left'>{0}</td>\n" +
"<td width='61%' align='right'>{1}</td>\n" +
"<td width='14%' align='right'><input type='text' name='T1' size='4' style='border-bottom:solid 1pt gray;border-top :solid 1pt gray; border-left:solid 1pt gray;border-right:solid 1pt gray;'> \n <input type='button' name='B1' size='6' value=go style='border-bottom:solid 1pt gray;border-top :solid 1pt gray; border-left:solid 1pt gray;border-right:solid 1pt gray;' ></td>\n" +
"</tr>\n" +
"</table>",leftInfo,
centerInfo.ToString(),
this.Pages);
writer.Write(sb.ToString());
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender (e);
if(!Page.IsClientScriptBlockRegistered("WEREW-332DFAF-FDAFDSFDSAFD"))
{
Page.RegisterClientScriptBlock("WEREW-332DFAF-FDAFDSFDSAFD",SCRIPTSTRING);
}
}
}
}