62,041
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 分页函数
/// </summary>
/// <param name="recordcount">总记录数</param>
/// <param name="pagesize">每页记录数</param>
/// <param name="currentpage">当前页数</param>
/// <returns></returns>
public static string HtmlPage(int recordcount, int pagesize, int currentpage)
{
string thisUrl = CreateUrl();
string pageStr = "";
int allcurrentpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
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;
//为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (startcount < 1)
startcount = 1;
//页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
if (allcurrentpage < endcount)
endcount = allcurrentpage;
//
pageStr += currentpage > 1 ? " <a href=\"" + thisUrl + "&page=1\" title=\"首页\">首页</a> <a href=\"" + thisUrl + "&page=" + pre + "\" title=\"前页\">前页</a>" : "首页 前页";
//if (startcount > 1) pageStr += currentpage > 1 ? " <a href=\"" + thisUrl + "&page=" + pre + "\" title=\"上一页\">上一页</a>" : "";
//当页码数大于1时, 则显示页码
if (endcount > 1)
{
//中间页处理, 这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
pageStr += currentpage == i ? " " + i + "" : " <a href=\"" + thisUrl + "&page=" + i + "\" >" + i + "</a>";
}
}
pageStr += currentpage != allcurrentpage ? " <a href=\"" + thisUrl + "&page=" + next + "\" title=\"后页\">后页</a> <a href=\"" + thisUrl + "&page=" + allcurrentpage + "\" title=\"尾页\">尾页</a> " : "后页 尾页";
//if (endcount < allcurrentpage) pageStr += currentpage != allcurrentpage ? " <a href=\"" + thisUrl + "&page=" + next + "\" title=\"下一页\">下一页</a> " : "";
if (endcount > 1)
pageStr += " ";
pageStr += " 页次:" + currentpage + "/" + allcurrentpage + " 页 " + pagesize + "个/页 ";
pageStr += "<select name=topage onChange=\"javascript:location.href=this.options[selectedIndex].value\">";
for (int i = 1; i <= allcurrentpage; i++)
{
pageStr += "<option value=\"" + thisUrl + "&page=" + i + "\" " + ((i == currentpage) ? "selected" : "") + " >" + i + "</option>";
}
pageStr += "</select>页";
pageStr += "共 " + recordcount + " 条记录";
return pageStr;
}
public static string CreateUrl()
{
string[] sortedStr = HttpContext.Current.Request.QueryString.AllKeys;
StringBuilder urlParam = new StringBuilder();
if (sortedStr.Count() > 0)
{
for (int i = 0; i < sortedStr.Length; i++)
{
if (ArhRequest.GetString(sortedStr[i]) != "" && sortedStr[i] != "page")
{
if (urlParam.ToString() == "")
urlParam.Append(sortedStr[i] + "=" + ArhRequest.GetString(sortedStr[i]));
else
urlParam.Append("&" + sortedStr[i] + "=" + ArhRequest.GetString(sortedStr[i]));
}
}
}
sortedStr = HttpContext.Current.Request.Form.AllKeys;
if (sortedStr.Count() > 0)
{
for (int i = 0; i < sortedStr.Length; i++)
{
if (ArhRequest.GetString(sortedStr[i]) != "" && sortedStr[i] != "page")
{
if (urlParam.ToString() == "")
urlParam.Append(sortedStr[i] + "=" + ArhRequest.GetString(sortedStr[i]));
else
urlParam.Append("&" + sortedStr[i] + "=" + ArhRequest.GetString(sortedStr[i]));
}
}
}
return HttpContext.Current.Request.FilePath + "?" + urlParam.ToString();
}