using system;
using system.web.ui;
using system.web.ui.webcontrols;
using system.componentmodel;
namespace pageinfocontrol
{
/// <summary>
/// webcustomcontrol1 的摘要说明。
/// </summary>
[defaultproperty(“totalrecord”),
toolboxdata(“<{0}:pageinfo runat=server></{0}:pageinfo>”)]
public class pageinfo : system.web.ui.webcontrols.webcontrol,ipostbackeventhandler
{
#region construct method
/// <summary>
/// 构造函数
/// </summary>
public pageinfo():base(htmltextwritertag.div)
{
}
#endregion
#region variables and constants
public event eventhandler changepageclick;
private string _barbackgroundcolor = “#f1f1f1”;
private string _barlinkcolor = “navy”;
private string _barcurrentcolor = “#eeeeee”;
private int _totalrecord = 0;
private int _totalpage = 0;
private int _pagesize = 0;
private int _currentpageindex = 1;
private int _itemsize = 10;
#endregion
#region properties
[
description(“分页条背景色”),
bindable(true),
category(“appearance”),
defaultvalue(“#f1f1f1”)
]
public string barbackgroundcolor
{
get{return _barbackgroundcolor;}
set{_barbackgroundcolor = value;}
}
[
description(“分页条带链接数字颜色”),
bindable(true),
category(“appearance”),
defaultvalue(“navy”)
]
public string barlinkcolor
{
get{return _barlinkcolor;}
set{_barlinkcolor = value;}
}
[
description(“分页条当前页数字颜色”),
bindable(true),
category(“appearance”),
defaultvalue(“#eeeeee”)
]
public string barcurrentcolor
{
get{return _barcurrentcolor;}
set{_barcurrentcolor = value;}
}
[
description(“总记录数”),
bindable(false),
category(“behavior”),
defaultvalue(0)
]
public int totalrecord
{
get{return _totalrecord;}
set
{
foreach(char c in system.convert.tostring(value))
{
if (!char.isnumber(c))
{
_totalrecord = 0;
break;
}
}
_totalrecord = value;
}
}
[
description(“每页显示记录数”),
bindable(true),
category(“behavior”),
defaultvalue(0)
]
public int pagesize
{
get{return _pagesize;}
set
{
foreach(char c in system.convert.tostring(value))
{
if (!char.isnumber(c))
{
_pagesize = 0;
break;
}
}
_pagesize = value;
}
}
[
description(“总页数”),
bindable(true),
category(“behavior”),
defaultvalue(0)
]
public int totalpage
{
get{return _totalpage;}
}
[
description(“数字规格”),
bindable(true),
category(“behavior”),
defaultvalue(10)
]
public int itemsize
{
get{return _itemsize;}
set
{
foreach(char c in system.convert.tostring(value))
{
if (!char.isnumber(c))
{
_itemsize = 10;
break;
}
}
_itemsize = value;
}
}
[
description(“当前页值”),
bindable(true),
category(“behavior”),
defaultvalue(1)
]
public int currentpageindex
{
get{return _currentpageindex;}
set{_currentpageindex = value;}
}
#endregion
//定义div的样式
protected override void addattributestorender(htmltextwriter writer)
{
writer.addstyleattribute(“white-space”,”nowrap”);
writer.addstyleattribute(“padding-top”,”2px”);
writer.addstyleattribute(“padding-bottom”,”2px”);
writer.addstyleattribute(“width”,width.tostring());
writer.addstyleattribute(“height”,height.tostring());
base.addattributestorender (writer);
}
protected virtual void onpagechangeclick(eventargs e)
{
if (changepageclick != null)
{
changepageclick(this, e);
}
}
public void raisepostbackevent(string eventargument)
{
int pageindex = int.parse(eventargument);
this._currentpageindex = pageindex;
onpagechangeclick(new eventargs());
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name=”output”> 要写出到的 html 编写器 </param>
protected override void rendercontents(htmltextwriter output)
{
this._totalpage = ((this.totalrecord / pagesize) * this.pagesize == this.totalrecord)?(this.totalrecord / this.pagesize):((this.totalrecord / this.pagesize) + 1);
int beginrecord = (this.currentpageindex – 1) * this.pagesize + 1;
int endrecord = this.currentpageindex * this.pagesize;
endrecord = (endrecord>this.totalrecord)?this.totalrecord:endrecord;
string pageinfo = “[共<font color=#cc0000>”+this.totalpage.tostring()+”</font>页/当前第<font color=#cc0000>”+this.currentpageindex.tostring()+”</font>页 共<font color=#cc0000>”+totalrecord.tostring()+”</font>条记录,当前记录数<font color=#cc0000>”+beginrecord.tostring()+”</font>到<font color=#cc0000>”+endrecord.tostring()+”</font>]”;
string pageliststr = “”;
string pageindexcolor = “#0000c0”;
int singlenumber = this.totalpage – (totalpage/itemsize) * itemsize; //得到分页后的尾数(比如:总共58页,按10页规格显示,则尾数为8)
int intpageformax = (this.currentpageindex – 1) / itemsize;
int minint = (1 + itemsize * intpageformax);
int maxint = ((intpageformax + 1) * itemsize)>totalpage?totalpage:((intpageformax + 1) * itemsize);
if(this.totalrecord == 0 || this.totalpage == 0)
{
pageliststr = “<font color=”+pageindexcolor+”>0</font>”;
pageliststr = pageliststr + ” [共<font color=#cc0000>0</font>页/当前第<font color=#cc0000>0</font>页 共<font color=#cc0000>0</font>条记录,当前记录数<font color=#cc0000>0</font>到<font color=#cc0000>0</font>]”;
output.write(pageliststr);
}
else
{
if(this.totalpage <= this.itemsize)
{
for(int i = 1;i <= totalpage;i++)
{
pageindexcolor = currentpageindex==i?”#cc0000″:”#0000c0″;
if(currentpageindex == i)
pageliststr = pageliststr + ” <a title=当前为第『”+i+”』页 href=# id=\”” + this.uniqueid + “\”><font color=”+pageindexcolor+”>”+i.tostring()+”</font></a>”;
else
pageliststr = pageliststr + ” <a title=点击转到第『”+i+”』页 id=\”” + this.uniqueid + “\” href=\”javascript:” + page.getpostbackeventreference(this,i.tostring()) +”\”><font color=”+pageindexcolor+”>”+i.tostring()+”</font></a>”;
}
pageliststr = pageliststr ==””?”<font color=”+pageindexcolor+”>0</font>”:pageliststr;
pageliststr = pageliststr + ” “+pageinfo;
output.write(pageliststr);
}
else
{
for(int i = minint;i <= maxint;i++)
{
pageindexcolor = currentpageindex==i?”#cc0000″:”#0000c0″;
if(currentpageindex == i)
pageliststr = pageliststr + ” <a title=当前为第『”+i+”』页 href=# id=\”” + this.uniqueid + “\”><font color=”+pageindexcolor+”>”+i.tostring()+”</font></a>”;
else
pageliststr = pageliststr + ” <a title=点击转到第『”+i+”』页 id=\”” + this.uniqueid + “\” href=\”javascript:” + page.getpostbackeventreference(this,i.tostring()) +”\”><font color=”+pageindexcolor+”>”+i.tostring()+”</font></a>”;
}
//当当前页数小于itemsize且总的页数大于itemsize时
if(currentpageindex <= itemsize && totalpage > itemsize)
{
pageliststr = pageliststr + ” <a id=\”” + this.uniqueid + “\” title=点击转到第『”+system.convert.tostring(itemsize + 1)+”』页 href=\”javascript:” + page.getpostbackeventreference(this,system.convert.tostring(itemsize + 1)) +”\”>>></a>”;
}
//当当前页数大于itemsize,且总的页数减去当前页数大于等于尾数值页数时
if(this.currentpageindex > itemsize && (totalpage – this.currentpageindex) >= singlenumber)
{
int multiminpageindex = (intpageformax * itemsize);
int multimaxpageindex = ((intpageformax+1) * itemsize) + 1;
pageliststr = “<a id=\”” + this.uniqueid + “\” title=点击转到第『”+multiminpageindex+”』页 href=\”javascript:” + page.getpostbackeventreference(this,multiminpageindex.tostring()) +”\”><<</a>” + pageliststr.trim() + “<a id=\”” + this.uniqueid + “\” title=点击转到第『”+multimaxpageindex+”』页 href=\”javascript:” + page.getpostbackeventreference(this,multimaxpageindex.tostring()) +”\”>>></a>”;
}
//当当前页数大于itemsize,且总的页数减去当前页数大于等于尾数值页数时
if(currentpageindex > 10 && (totalpage – currentpageindex) < singlenumber)
{
int multiminpageindex = (intpageformax * itemsize);
pageliststr = “<a id=\”” + this.uniqueid + “\” title=点击转到第『”+multiminpageindex+”』页 href=\”javascript:” + page.getpostbackeventreference(this,multiminpageindex.tostring()) +”\”><<</a>” + pageliststr.trim();
}
pageliststr = pageliststr ==””?”<font color=”+pageindexcolor+”>0</font>”:pageliststr;
pageliststr = pageliststr + ” “+pageinfo;
output.write(pageliststr);
}
}
base.rendercontents(output);
}
}
}