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;' onclick='go(T1,{2})'></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);
}
}
}
}
本文地址:http://com.8s8s.com/it/it44417.htm