欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 分页链接方法

程序员文章站 2024-03-07 21:15:33
复制代码 代码如下:/// /// 分页链接 /// ///
复制代码 代码如下:

/// <summary>
/// 分页链接
/// </summary>
/// <param name="pagesize"></param>
/// <param name="recordcount"></param>
/// <param name="currentpage"></param>
/// <param name="prev">当前页前面显示的数量</param>
/// <param name="next">当前页后面显示的数量</param>
/// <returns></returns>
public string pagelink(int pagesize, int recordcount, int currentpage, int prev, int next)
{
int pagecount = recordcount % pagesize == 0 ? (recordcount / pagesize) : ((int)math.ceiling((double)recordcount / pagesize));
stringbuilder sb = new stringbuilder();
if (currentpage > 1 && recordcount > 1)
{
sb.append("<a href=\"?page=");
sb.append((currentpage - 1).tostring());
sb.append("\">前一页</a>  ");
}
if (currentpage > prev + 1)
sb.append("<a href=\"?page=1\">1</a> ... ");
if (currentpage < prev)
next = next + prev - currentpage + 1;
if (next > pagecount - currentpage)
prev = prev + next - (pagecount - currentpage);
for (int i = 1; i <= pagecount; i++)
{
if (i == currentpage)
{
sb.append("<a href=\"?page=" + i + "\" class=\"current\" ><font color=\"red\">" + i + "</font></a>  ");
}
else
{
if (i > (currentpage - prev - 1) && i < (currentpage + next + 1))
{
sb.append("<a href=\"?page=" + i + "\">" + i + "</a>  ");
}
}
}
if (currentpage < pagecount - next)
sb.append("... <a href=\"?page=" + pagecount.tostring() + "\">" + pagecount.tostring() + "</a>");
if (currentpage < pagecount)
sb.append("  <a href=\"?page=" + (currentpage + 1).tostring() + "\">后一页</a>");
return sb.tostring();
}