ASP.Net 分页控件源码
程序员文章站
2022-10-12 19:32:07
特性: 、支持datagrid,datalist等分种数据显示控件,理论上只要有datasource的控件都可以 、支持url方式分页,url分页的每一页都可以被搜索引擎搜...
特性:
、支持datagrid,datalist等分种数据显示控件,理论上只要有datasource的控件都可以
、支持url方式分页,url分页的每一页都可以被搜索引擎搜到,并且支持url重写
、控件样式可自定义
、封装完全,使用过程序只需写一行代码
复制代码 代码如下:
/******************************************************************
** 文件名:pager.cs
** 创建人:杨响武
** 日 期:2005年7月27日
** 修改人:
** 日 期:
** 描 述:
**
**----------------------------------------------------------------
******************************************************************/
using system;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.web;
using system.web.ui;
using system.text.regularexpressions;
namespace mycompany.controls
{
/// <summary>
/// 分页控件
/// </summary>
public class pager : label, inamingcontainer
{
#region 成员变量和构造函数
httpcontext context = httpcontext.current;
label countlabel;
hyperlink previousbutton;
hyperlink nextbutton;
hyperlink firstbutton;
hyperlink lastbutton;
hyperlink[] paginglinkbuttons;
public override controlcollection controls
{
get
{
ensurechildcontrols();
return base.controls;
}
}
protected override void createchildcontrols()
{
controls.clear();
addcountlabel();
addpagebuttons();
addpreviousnexthyperlinks();
addfirstlasthyperlinks();
}
#endregion
#region 呈现方法
protected override void render(htmltextwriter writer)
{
// 修改总页数小于等于1的时候不呈现任何控件
// int totalpages = calculatetotalpages();
//
// if (totalpages <= 1)
// return;
addattributestorender(writer);
writer.addattribute(htmltextwriterattribute.class, this.cssclass, false);
rendercountlabel(writer);
renderfirst(writer);
renderprevious(writer);
renderpagingbuttons(writer);
rendernext(writer);
renderlast(writer);
}
void rendercountlabel(htmltextwriter writer)
{
countlabel.rendercontrol(writer);
literalcontrol l = new literalcontrol(" ");
l.rendercontrol(writer);
}
void renderfirst (htmltextwriter writer)
{
firstbutton.rendercontrol(writer);
literalcontrol l = new literalcontrol(" ");
l.rendercontrol(writer);
}
void renderlast (htmltextwriter writer)
{
lastbutton.rendercontrol(writer);
literalcontrol l = new literalcontrol(" ");
l.rendercontrol(writer);
}
void renderprevious (htmltextwriter writer)
{
previousbutton.rendercontrol(writer);
literalcontrol l = new literalcontrol(" ");
l.rendercontrol(writer);
}
void rendernext(htmltextwriter writer)
{
nextbutton.rendercontrol(writer);
literalcontrol l = new literalcontrol(" ");
l.rendercontrol(writer);
}
private void renderbuttonrange(int start, int end, htmltextwriter writer)
{
for (int i = start; i < end; i++)
{
if (pageindex == i)
{
literal l = new literal();
l.text = "<font color =red>" + (i + 1).tostring() + "</font>";
l.rendercontrol(writer);
}
else
{
paginglinkbuttons.rendercontrol(writer);
}
if (i < (end - 1))
writer.write(" ");
}
literalcontrol l1 = new literalcontrol(" ");
l1.rendercontrol(writer);
}
private void renderpagingbuttons(htmltextwriter writer)
{
int totalpages;
totalpages = calculatetotalpages();
if (totalpages <= 10)
{
renderbuttonrange(0, totalpages, writer);
}
else
{
int lowerbound = (pageindex - 4);
int upperbount = (pageindex + 6);
if (lowerbound <= 0)
lowerbound = 0;
if (pageindex <= 4)
renderbuttonrange(0, 10, writer);
else if (pageindex < (totalpages - 5))
renderbuttonrange(lowerbound, (pageindex + 6), writer);
else if (pageindex >= (totalpages - 5))
renderbuttonrange((totalpages - 10), totalpages, writer);
}
}
#endregion
#region 控件树方法
/// <summary>
/// 信息标签
/// </summary>
void addcountlabel()
{
countlabel = new label();
countlabel.id = "countlabel";
countlabel.text = string.format(text, calculatetotalpages().tostring("n0"));
controls.add(countlabel);
}
private void addpagebuttons()
{
paginglinkbuttons = new hyperlink[calculatetotalpages()];
for (int i = 0; i < paginglinkbuttons.length; i++)
{
paginglinkbuttons = new hyperlink();
paginglinkbuttons.enableviewstate = false;
paginglinkbuttons.text = (i + 1).tostring();
paginglinkbuttons.id = i.tostring();
paginglinkbuttons.cssclass = linkcss;
paginglinkbuttons.navigateurl = gethrefstring(i);
controls.add(paginglinkbuttons);
}
}
/// <summary>
/// 首页末页
/// </summary>
void addfirstlasthyperlinks()
{
firstbutton = new hyperlink();
firstbutton.id = "first";
firstbutton.text = "首页";
if(pageindex != 0 && calculatetotalpages() > 0)
{
firstbutton.navigateurl = gethrefstring(0);
}
else
{
firstbutton.enabled = false;
}
controls.add(firstbutton);
lastbutton = new hyperlink();
lastbutton.id = "last";
lastbutton.text = "末页";
if(pageindex != calculatetotalpages() - 1)
{
lastbutton.navigateurl = gethrefstring(calculatetotalpages() - 1);
}
else
{
lastbutton.enabled = false;
}
controls.add(lastbutton);
}
/// <summary>
/// 上一页下一页
/// </summary>
void addpreviousnexthyperlinks()
{
previousbutton = new hyperlink();
previousbutton.id = "prev";
previousbutton.text = "上一页";
if (hasprevious)
{
previousbutton.navigateurl = gethrefstring(pageindex - 1);
}
else
{
previousbutton.enabled = false;
}
controls.add(previousbutton);
nextbutton = new hyperlink();
nextbutton.id = "next";
nextbutton.text = "下一页";
if(hasnext)
{
nextbutton.navigateurl = gethrefstring(pageindex + 1);
}
else
{
nextbutton.enabled = false;
}
controls.add(nextbutton);
}
#endregion
#region 私有属性
private bool hasprevious
{
get
{
if (pageindex > 0)
return true;
return false;
}
}
private bool hasnext
{
get
{
if (pageindex + 1 < calculatetotalpages() )
return true;
return false;
}
}
#endregion
#region 帮助方法和公共属性
/// <summary>
/// 获取分页导航按钮的超链接字符串
/// </summary>
/// <param name="pageindex">该分页按钮相对应的页索引</param>
/// <returns>分页导航按钮的超链接字符串</returns>
private string gethrefstring(int pageindex)
{
string url = page.request.rawurl;
if (regex.ismatch(url, @"(\d+).shtml", regexoptions.ignorecase))
{
url = regex.replace(url, @"(\d+).shtml", pageindex + ".shtml");
}
if (regex.ismatch(url, @"pageindex=(\d+)", regexoptions.ignorecase))
{
url = regex.replace(url, @"pageindex=(\d+)", "pageindex=" + pageindex.tostring());
}
return page.server.urlpathencode(url);
}
public int calculatetotalpages()
{
int totalpagesavailable;
if (totalrecords == 0)
return 0;
totalpagesavailable = totalrecords / pagesize;
if ((totalrecords % pagesize) > 0)
totalpagesavailable++;
return totalpagesavailable;
}
/// <summary>
/// 当前页
/// </summary>
public int pageindex
{
get
{
int _pageindex = 0;
if ((!page.ispostback) && (context.pageindex > 0))
_pageindex = context.pageindex;
else
_pageindex = convert.toint32(viewstate["pageindex"]);
if (_pageindex < 0)
return 0;
else
{
if (_pageindex > calculatetotalpages()-1)
return calculatetotalpages()-1;
else
return _pageindex;
}
}
set
{
viewstate["pageindex"] = value;
}
}
/// <summary>
/// 每页记录数
/// </summary>
public int pagesize
{
get
{
int pagesize = convert.toint32(viewstate["pagesize"]);
if (pagesize == 0)
{
return 10;
}
return pagesize;
}
set
{
viewstate["pagesize"] = value;
}
}
/// <summary>
/// 总记录数
/// </summary>
public int totalrecords
{
get
{
return convert.toint32(viewstate["totalrecords"]);
}
set
{
viewstate["totalrecords"] = value;
}
}
private string text = "共{0}页";
private string linkcss = "";
/// <summary>
/// 链接样式
/// </summary>
public string linkcss
{
get { return linkcss; }
set { linkcss = value;}
}
private string textcss = "";
/// <summary>
/// 文字样式
/// </summary>
public string textcss
{
get { return textcss; }
set { textcss = value;}
}
public string textformat
{
get
{
return text;
}
set
{
text = value;
}
}
#endregion
}
}
在页面上只需要向下面这样写:
复制代码 代码如下:
<%@ control language="c#" %>
<%@ register tagprefix="anchor" namespace="mycompany.controls" assembly="mycompany.controls" %>
<anchor:pager id="pager1" runat="server"></anchor:pager>
上一篇: 太极拳新手必知 太极拳六个基本功
下一篇: 孕妇能吃方便面吗 要摒弃不健康饮食习惯
推荐阅读