public static string Pagination(int recordcount, int pagesize, int currentpage, string url)
{
int allcurrentpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string currentpagestr = "";
if (currentpage < 1)
{ currentpage = 1; }
//计算总页数
if (pagesize != 0)
{
allcurrentpage = (recordcount / pagesize); //计算页数
allcurrentpage = ((recordcount % pagesize) != 0 ? allcurrentpage + 1 : allcurrentpage);
allcurrentpage = (allcurrentpage == 0 ? 1 : allcurrentpage);
}
next = currentpage + 1;
pre = currentpage - 1;
startcount = (currentpage + 5) > allcurrentpage ? allcurrentpage - 9 : currentpage - 4;//中间页起始序号
//中间页终止序号
endcount = currentpage < 5 ? 10 : currentpage + 5;
if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (allcurrentpage < endcount) { endcount = allcurrentpage; }//页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
currentpagestr = "共" + allcurrentpage + "页 ";
currentpagestr += currentpage > 1 ? "<a href=\"" + url + "currentpage=1\">首页</a> <a href=\"" + url + "currentpage=" + pre + "\">上一页</a>" : "首页 上一页";
//中间页处理,这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
currentpagestr += currentpage == i ? " <font color=\"#ff0000\">" + i + "</font>" : " <a href=\"" + url + "currentpage=" + i + "\">" + i + "</a>";
}
currentpagestr += currentpage != allcurrentpage ? " <a href=\"" + url + "currentpage=" + next + "\">下一页</a> <a href=\"" + url + "currentpage=" + allcurrentpage + "\">末页</a>" : " 下一页 末页";
// PageLink = currentpagestr;
return currentpagestr;
}
效果示意图