asp.net web页面自定义分页控件使用详解
程序员文章站
2023-12-23 11:19:34
这几天学习了一下自定义分页控件,现将实现方法记录下来,亲测可以实现:
1.首先创建一个.ascx文件,命名为turnpage,然后在里面写控件前台展示的界面:...
这几天学习了一下自定义分页控件,现将实现方法记录下来,亲测可以实现:
1.首先创建一个.ascx文件,命名为turnpage,然后在里面写控件前台展示的界面:
2.然后在turnpage.ascx.cs里面写相应的后台代码,代码如下:
namespace web { public delegate void gotopage(int pagenum); public partial class turnpage : system.web.ui.usercontrol { private gotopage _gotopage = null; protected void page_load(object sender, eventargs e) { } public void initcontrol(gotopage gp) { _gotopage = gp; } public int datacount { get { return int32.parse(lbl_totalcount.text); } set { lbl_totalcount.text = value.tostring(); } } public int currpagenum { get { return int32.parse(lbl_currpage.text); } set { lbl_currpage.text = value.tostring(); } } public int totalpagenum { get { return int32.parse(lbl_totalpage.text); } set { lbl_totalpage.text = value.tostring(); } } public int pagesize { get { return int32.parse(ddl_pagesize.selectedvalue); } } protected void btn_fristpage_click(object sender, eventargs e) { _gotopage(1); } protected void btn_prevpage_click(object sender, eventargs e) { if (int.parse(lbl_currpage.text) > 1) _gotopage(int.parse(lbl_currpage.text) - 1); else _gotopage(1); } protected void btn_nextpage_click(object sender, eventargs e) { if (int.parse(lbl_currpage.text) < int.parse(lbl_totalpage.text)) _gotopage(int.parse(lbl_currpage.text) + 1); else _gotopage(int.parse(lbl_totalpage.text)); } protected void btn_lastpage_click(object sender, eventargs e) { _gotopage(int.parse(lbl_totalpage.text)); } public void controlbuttonclick() { if (datacount > 0) { btn_fristpage.enabled = true; btn_prevpage.enabled = true; btn_lastpage.enabled = true; btn_nextpage.enabled = true; } else { btn_fristpage.enabled = false; btn_prevpage.enabled = false; btn_lastpage.enabled = false; btn_nextpage.enabled = false; } if (currpagenum == 1) { btn_fristpage.enabled = false; btn_prevpage.enabled = false; } if (currpagenum == totalpagenum) { btn_lastpage.enabled = false; btn_nextpage.enabled = false; } if (currpagenum == 0) { btn_fristpage.enabled = false; btn_prevpage.enabled = false; btn_lastpage.enabled = false; btn_nextpage.enabled = false; } } protected void ddl_pagesize_selectedindexchanged(object sender, eventargs e) { _gotopage(1); } protected void btn_go_click(object sender, eventargs e) { int pagenum; if (int.tryparse(txt_pagenum.text, out pagenum)) { if (pagenum > totalpagenum) _gotopage(totalpagenum); else if (pagenum < 1) _gotopage(1); else _gotopage(pagenum); } } }}
3.控件的方法就写好了,现在要做的就是在其它页面引用这个控件了,引用方法如下:
然后在页尾还需配置一下:
4.这就是最后一步了,在你引用这个控件的页面的后台代码里写下以下语句:
大功告成了!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。