模拟google分页效果

时间:2022-02-10 23:38:27
 /// </summary>
/// <param name="total">总记录数</param>
/// <param name="per">每页记录数</param>
/// <param name="page">当前页数</param>
/// <param name="query_string">Url参数</param>
private string pagination(int total, int per, int page, string query_string)
{
int allpage = ;
int next = ;
int pre = ;
int startcount = ;
int endcount = ;
string pagestr = ""; if (page < ) { page = ; }
//计算总页数
if (per != )
{
allpage = (total / per);
allpage = ((total % per) != ? allpage + : allpage);
allpage = (allpage == ? : allpage);
}
next = page + ;
pre = page - ;
startcount = (page + ) > allpage ? allpage - : page - ;//中间页起始序号
//中间页终止序号
endcount = page < ? : page + ;
if (startcount < ) { startcount = ; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (allpage < endcount) { endcount = allpage; }//页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
pagestr = "共" + allpage + "页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; pagestr += page > ? "<a href=\"" + query_string + "?page=1\">首页</a>&nbsp;&nbsp;<a href=\"" + query_string + "?page=" + pre + "\">上一页</a>" : "首页 上一页";
//中间页处理,这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? "&nbsp;&nbsp;<font color=\"#ff0000\">" + i + "</font>" : "&nbsp;&nbsp;<a href=\"" + query_string + "?page=" + i + "\">" + i + "</a>";
}
pagestr += page != allpage ? "&nbsp;&nbsp;<a href=\"" + query_string + "?page=" + next + "\">下一页</a>&nbsp;&nbsp;<a href=\"" + query_string + "?page=" + allpage + "\">末页</a>" : " 下一页 末页"; return pagestr;
}