首页 > 编程 > .NET > 正文

ASP.NET分页组件学与用——教学篇(源代码)

2024-07-10 12:57:31
字体:
来源:转载
供稿:网友
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() + "&nbsp;&nbsp;" + "每页" + this.count.tostring() + "条" + "&nbsp;&nbsp;共" + 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 = "&nbsp;";//循环生成数字序列部分

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);

}

}



}

}

最大的网站源码资源下载站,

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表