分页控件源代码--组合控件
2024-07-21 02:24:40
供稿:网友
using system;
using system.io;
using system.drawing;
using system.data;
using system.data.sqlclient;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.componentmodel;
namespace pager
{
?///
?/// coolpager 的摘要说明。
?///
?public enum sorttype
?{
??asc=0,desc=1
?};
?public enum iscount
?{
??noreturnrecordcount=0,returnrecordcount
?};
?[defaultproperty("copyright"),
??toolboxdata("<{0}:coolpager runat=server>")]
?public class coolpager : system.web.ui.webcontrols.webcontrol,inamingcontainer
?{?
??protected int recordcount=0;
??protected control _controltopaginate;
??protected pageddatasource _datasource;
??[bindable(true),
???category("appearance"),
???defaultvalue("")]
??public string copyright
??{
???get
???{
????return "版权所有:千山一鸟";
???}
??}
??//连接字符串
??[description("数据源连接字符串"),defaultvalue("")]
??public string connectionstring
??{
???get
???{
????object obj = viewstate["connectionstring"];
????return (obj==null)?string.empty:obj.tostring();
???}
???set
???{
????viewstate["connectionstring"]=value;
???}
??}
??//显示分页的控件
??[description("用于显示分页的控件"),defaultvalue("")]
??public string controltopaginate
??{
???get
???{
????object obj=viewstate["controltopaginate"];
????return (obj==null)?string.empty:obj.tostring();
???}
???set
???{
????viewstate["controltopaginate"]=value;
???}
??}
??//表名
??[description("查询表名"),defaultvalue("")]
??public string tablename
??{
???get
???{
????object obj=viewstate["tablename"];
????return (obj==null)?string.empty:obj.tostring();
???}
???set
???{
????viewstate["tablename"]=value;
???}
??}
??//排序字段名
??[description("排序字段"),defaultvalue("")]
??public string fieldname
??{
???get
???{
????object obj=viewstate["fieldname"];
????return (obj==null)?string.empty:obj.tostring();
???}
???set
???{
????viewstate["fieldname"]=value;
???}
??}
??//每页显示记录数
??[description("每页记录数"),defaultvalue("")]
??public int pagesize
??{
???get
???{
????object obj=viewstate["pagesize"];
????return (obj==null)?15:convert.toint32(obj);
???}
???set
???{
????viewstate["pagesize"]=value;
???}
??}
??//页码
??[description("当前页码"),defaultvalue("")]
??public int pageindex
??{
???get
???{
????object obj=viewstate["pageindex"];
????return (obj==null)?1:convert.toint32(obj);
???}
???set
???{
????viewstate["pageindex"]=value;
???}
??}
??//是否返回记录总数
??[description("是否返回记录总数"),defaultvalue("")]
??public iscount iscount
??{
???get
???{
????return iscount.returnrecordcount;
???}
??}
??//排序类型
??[description("排序类型"),defaultvalue("")]
??public sorttype sorttype
??{
???get
???{
????object obj=viewstate["sorttype"];
????return (obj==null)?sorttype.asc:(sorttype)obj;
???}
???set
???{
????viewstate["sorttype"]=value;
???}
??}
??//where子句
??[description("where子句,不带where"),defaultvalue("")]
??public string where
??{
???get
???{
????object obj=viewstate["where"];
????return (obj==null)?string.empty:obj.tostring();
???}
???set
???{
????viewstate["where"]=value;
???}
??}
??[description("显示导航数字数"),defaultvalue("")]
??public int showpagenumber
??{
???get
???{
????object obj=viewstate["showpagenumber"];
????return (obj==null)?10:convert.toint32(obj);
???}
???set
???{
????viewstate["showpagenumber"]=value;
???}
??}
??[description("开始页"),defaultvalue("")]
??public int pagestart
??{
???get
???{
????object obj=viewstate["pagestart"];
????return (obj==null)?1:convert.toint32(obj);
???}
???set
???{
????viewstate["pagestart"]=value;
???}
??}
??[description("总页数,自动生成"),defaultvalue("")]
??public int pagecount
??{
???get
???{
????object obj=viewstate["pagecount"];
????return (obj==null)?1:convert.toint32(obj);
???}
???set
???{
????viewstate["pagecount"]=value;
???}
??}
??///
??/// 将此控件呈现给指定的输出参数。
??///
??/// 要写出到的 html 编写器
??protected override void render(htmltextwriter output)
??{
???if(page.enableviewstate==false)
????page.enableviewstate=true;
???if(site!=null && site.designmode)
????if(!page.ispostback)
????createchildcontrols();
???base.render(output);
??}
??public override void databind()
??{
???base.databind();
???childcontrolscreated = false;
???if (controltopaginate == "")
????return;
???_controltopaginate = page.findcontrol(controltopaginate);
???if (_controltopaginate == null)
????return;
???if (!(_controltopaginate is basedatalist || _controltopaginate is listcontrol))
????return;
???if (connectionstring == "" || tablename == "")
????return;
???// fetch data
???fetchdata();
???// bind data to the buddy control
???basedatalist basedatalistcontrol = null;
???listcontrol listcontrol = null;
???if (_controltopaginate is basedatalist)
???{
????basedatalistcontrol = (basedatalist) _controltopaginate;
????basedatalistcontrol.datasource = _datasource;
????basedatalistcontrol.databind();
????return;
???}
???if (_controltopaginate is listcontrol)
???{
????listcontrol = (listcontrol) _controltopaginate;
????listcontrol.items.clear();
????listcontrol.datasource = _datasource;
????listcontrol.databind();
????return;
???}
??}
??private void fetchdata()
??{
???sqlconnection cn=new sqlconnection(connectionstring);
???sqlcommand cm=new sqlcommand();
???cm.connection=cn;
???cm.connection.open();
???cm.commandtext="getrecordfrompage";
???cm.commandtype=commandtype.storedprocedure;
???cm.parameters.add("@tblname",sqldbtype.nvarchar,255);
???cm.parameters.add("@fldname",sqldbtype.nvarchar,255);
???cm.parameters.add("@pagesize",sqldbtype.int);
???cm.parameters.add("@pageindex",sqldbtype.int);
???cm.parameters.add("@iscount",sqldbtype.bit);
???cm.parameters.add("@ordertype",sqldbtype.bit);
???cm.parameters.add("@strwhere",sqldbtype.nvarchar,1000);
???cm.parameters["@tblname"].value=tablename;
???cm.parameters["@fldname"].value=fieldname;
???cm.parameters["@pagesize"].value=pagesize;
???cm.parameters["@pageindex"].value=pageindex;
???cm.parameters["@iscount"].value=0;
???cm.parameters["@ordertype"].value=sorttype;
???cm.parameters["@strwhere"].value=where;
???sqldataadapter adapter = new sqldataadapter(cm);
???datatable dtb = new datatable();
???adapter.fill(dtb);
???cm.parameters["@iscount"].value=iscount;
???recordcount=(int)cm.executescalar();
???pagecount=(int)math.ceiling((double)recordcount/pagesize);
???cm.connection.close();
???cm.dispose();
???cn.dispose();
???adapter.dispose();
???// configures the paged data source component
???if (_datasource == null)
????_datasource = new pageddatasource();
???_datasource.allowcustompaging = true;
???_datasource.allowpaging = true;
???_datasource.currentpageindex = 0;
???if(pageindex==pagecount)
???{
????_datasource.pagesize = (recordcount-(pageindex-1)*pagesize);
???}
???else
???{
????_datasource.pagesize = pagesize;
???}
???if(pageindex==pagecount)
???{
????_datasource.virtualcount = (recordcount-(pageindex-1)*pagesize);
???}
???else
???{
????_datasource.virtualcount = pagesize;
???}
???_datasource.datasource = dtb.defaultview;
??}
??protected override void createchildcontrols()
??{
???controls.clear();
???if(!page.ispostback)
???{
????clearchildviewstate();
???}
???//***********************************************
???table table=new table();
???table.width=unit.percentage(100);
???table.cellpadding=0;
???table.cellspacing=3;
???table.borderwidth=0;
???table.font.size=fontunit.point(9);
???tablerow tr=new tablerow();
???tablecell tdinfo=new tablecell();
???tdinfo.width=unit.percentage(25);
???tdinfo.horizontalalign=horizontalalign.left;
???buildinfo(tdinfo);
???tr.cells.add(tdinfo);
???tablecell tdnavigate=new tablecell();
???tdnavigate.width=unit.percentage(50);
???tdnavigate.horizontalalign=horizontalalign.center;
???buildnavigator(tdnavigate);
???tr.cells.add(tdnavigate);
???tablecell tdgoto=new tablecell();
???tdgoto.width=unit.percentage(25);
???tdgoto.horizontalalign=horizontalalign.right;
???buildgoto(tdgoto);
???tr.cells.add(tdgoto);
???table.rows.add(tr);
???controls.add(table);
??}
??private void buildinfo(tablecell td)
??{
???literalcontrol lc=new literalcontrol();
???lc.text="页码:"+pageindex.tostring()+"|"+pagecount.tostring()+" 记录:"+((pageindex-1)*pagesize+1)+"-"+((recordcount???td.controls.add(lc);
??}
??private void buildnavigator(tablecell td)
??{
???int temp;
???if(pageindex>showpagenumber)
???{
????linkbutton lbback=new linkbutton();
????lbback.causesvalidation=false;
????lbback.text="...";
????lbback.commandargument="back";
????lbback.command +=new commandeventhandler(lb_click);
????td.controls.add(lbback);
????td.controls.add(new literalcontrol(" "));
???}
???//*************************
???for(int i=0;i???{
????if((pagestart+i)<=pagecount)
????{
?????temp=pagestart+i;
?????linkbutton lbnumber=new linkbutton();
?????lbnumber.causesvalidation=false;
?????lbnumber.text=temp.tostring();
?????lbnumber.commandargument=temp.tostring();
?????lbnumber.command +=new commandeventhandler(lbnumber_click);
?????td.controls.add(lbnumber);
?????td.controls.add(new literalcontrol(" "));
????}
???}
???if((pagecount-pagestart)>showpagenumber)
???{
????linkbutton lbforward=new linkbutton();
????lbforward.causesvalidation=false;
????lbforward.text="...";
????lbforward.commandargument="forward";
????lbforward.command +=new commandeventhandler(lb_click);
????td.controls.add(lbforward);
???}
??}
??private void buildgoto(tablecell td)
??{
???textbox tb=new textbox();
???tb.id="gotopage";
???tb.borderwidth=unit.pixel(1);
???tb.width=unit.pixel(30);
???tb.enableviewstate=true;
???td.controls.add(tb);
???literalcontrol lc=new literalcontrol(" ");
???td.controls.add(lc);
???button btn=new button();
???btn.text="goto";
???btn.commandargument=tb.clientid;
???btn.command +=new commandeventhandler(lb_click);
???td.controls.add(btn);
???requiredfieldvalidator val2=new requiredfieldvalidator();
???val2.controltovalidate="gotopage";
???val2.errormessage="页码不能为空!";
???val2.display=validatordisplay.none;
???val2.id="val2";
???td.controls.add(val2);
???rangevalidator val1=new rangevalidator();
???val1.display=validatordisplay.none;
???val1.errormessage="无效的页码!";
???val1.id="val1";
???val1.controltovalidate="gotopage";
???val1.type=validationdatatype.integer;
???val1.minimumvalue="1";
???if(pagecount<1)
???{
????val1.maximumvalue="1";
???}
???else
???{
????val1.maximumvalue=pagecount.tostring();
???}
???
???td.controls.add(val1);
???validationsummary valall=new validationsummary();
???valall.showmessagebox=true;
???valall.showsummary=false;
???td.controls.add(valall);
??}
??private void lb_click(object sender,commandeventargs e)
??{
???switch(e.commandargument.tostring())
???{
????case "back":
?????pagestart-=showpagenumber;
?????pageindex=pagestart;
?????createchildcontrols();
?????break;
????case "forward":
?????pagestart+=showpagenumber;
?????pageindex=pagestart;
?????createchildcontrols();
?????break;
????default:
?????//textbox tb=(textbox)parent.findcontrol("coolpager1:gotopage");
??????? //pageindex=convert.toint32(tb.text);
?????string temp=page.request.form[this.clientid+":gotopage"].tostring().trim();
?????try
?????{
??????pageindex=convert.toint32(temp);
??????if(pageindex % showpagenumber==0)
??????{
???????pagestart=pageindex+1-showpagenumber;
??????}
??????else
??????{
???????pagestart=(int)math.floor((double)pageindex/showpagenumber)*showpagenumber+1;
??????}
??????createchildcontrols();
?????}
?????catch
?????{
?????}
?????break;
???}
???databind();
??}
??private void lbnumber_click(object sender,commandeventargs e)
??{
???pageindex=convert.toint32(e.commandargument);
???databind();
??}
?}
}
中国最大的web开发资源网站及技术社区,