public class SplitManager
{
public static string AspNetPagers(Pager pager)
{
StringBuilder sb = new StringBuilder();
string attr="";
int pagecount = 0;//当前页面的总层数
int floorcount = 0;//分页的总层数
int currentLastPage = 0;//当前最后一页的页码
int pageNum = pager.RecordCount / pager.PageSize + 1;//总页数 1~24
sb.AppendFormat("<div class=https://www.jb51.net/article/\"{0}\">\n", pager.ClassName);
attr=pager.PageIndex==1?"disabled=disabled":"";//标志当前页第一页是否相等 来控制前俩个按钮的有效性
sb.AppendFormat(GetAHtml(attr,string.Format(pager.URL,1),pager.FirstPageText));//添加最前一页的代码
sb.AppendFormat(GetAHtml(attr, string.Format(pager.URL, pager.PageIndex-1), pager.PrePageText));//添加上一页的代码
pagecount = pager.PageIndex/pager.MaxShowPageSize;//当前页数 0~1~2
pagecount = pager.PageIndex % pager.MaxShowPageSize == 0 ? pagecount - 1 : pagecount;//清除当 当前页数为分页页码数的整数倍页时除数多一的状况
floorcount = pageNum / pager.MaxShowPageSize;//页面层数 0~1~2
currentLastPage=pageNum<pager.MaxShowPageSize * (pagecount + 1)?pageNum:pager.MaxShowPageSize * (pagecount + 1);
if (pager.PageIndex > pager.MaxShowPageSize)//当当前序号大于每页页码个数时显示再前端...
{
sb.AppendFormat(GetAHtml("", string.Format(pager.URL, pager.MaxShowPageSize * pagecount), "..."));
}
for (int i = pager.MaxShowPageSize * pagecount + 1; i <= currentLastPage; i++)
{
if (i == pager.PageIndex)//判断循环页面是否为当前页
{
sb.AppendFormat(GetSpanHtml(i, pager.CurrentPageButtonCss));
}
else {
sb.AppendFormat(GetAHtml("", string.Format(pager.URL, i),i.ToString()));
}
}
if (pager.PageIndex <= pager.MaxShowPageSize * floorcount)//当当前序号小于倒数第二页页码时显示在后端...
{
sb.AppendFormat(GetAHtml("", string.Format(pager.URL, pager.MaxShowPageSize * (pagecount + 1) + 1), "..."));
}
attr = pager.PageIndex == pageNum ? "disabled=disabled" : "";//标志当前页最后一页是否相等 来控制后俩个按钮的有效性
sb.AppendFormat(GetAHtml(attr, string.Format(pager.URL, pager.PageIndex+1), pager.NextPageText));//添加后一页的代码
sb.AppendFormat(GetAHtml(attr, string.Format(pager.URL, pageNum), pager.LastPageText));//添加最后一页的代码
if (pager.ShowSpanText)//是否显示分页文字span标签显示
{
sb.AppendFormat("<span class=https://www.jb51.net/article/\"" + pager.SpanTextClass + "https://www.jb51.net/article/\">共{0}页,每页{1}条记录 \n", pageNum, pager.PageSize);
sb.AppendFormat("到第<input type=https://www.jb51.net/article/\"input\" id=https://www.jb51.net/article/\"jumpNum\" style=https://www.jb51.net/article/\"width:20px;\" name=https://www.jb51.net/article/\"jump\" value=https://www.jb51.net/article/\"{0}\" />页", pager.PageIndex == pageNum ? pageNum : pager.PageIndex + 1);
sb.AppendFormat("<a href=https://www.jb51.net/article/\"#\" style=https://www.jb51.net/article/\"float:none;\" onclick=https://www.jb51.net/article/\"javascript:jump();\">" + pager.SubmitButtonText + "</a></span>\n");
sb.Append(GetJumpScript(pager.URL));//添加按钮跳转的javascript代码
}
sb.AppendFormat("</div>");//
return sb.ToString();
}
/// <summary>
/// get the html of a label
/// </summary>
/// <param>a's title</param>
/// <param>the url of a</param>
/// <param>the attribute</param>
/// <returns>return html string</returns>
private static string GetAHtml(string attr,string url,string title)
{
return "<a " + attr + " href=https://www.jb51.net/article/\""+url+"https://www.jb51.net/article/\" style=https://www.jb51.net/article/\"margin-right:5px;\">"+title+"</a>\n";
}
/// <summary>
/// get the html of a label
/// </summary>
/// <param>the content of span</param>
/// <param>Class style Name</param>
/// <returns>return html string </returns>
private static string GetSpanHtml(int num, string className)
{
return "<span class=https://www.jb51.net/article/\"" + className + "https://www.jb51.net/article/\">" + num + "</span>\n";
}
/// <summary>
/// 获取跳转的javascript代码
/// </summary>
/// <param>当前分页的url规则</param>
/// <returns>返回一个javascript代码</returns>
private static string GetJumpScript(string url)
{
string scriptstr = "<script type=https://www.jb51.net/article/\"text/javascript\">\n" +
"function jump(){\n" +
"var jnum=document.getElementById(\"jumpNum\").value;\n" +
"if(isNaN(jnum)){\n"+
"alert(\"在跳转框中请输入数字!\");\n" +
"}\n"+
"else{\n"+
//"alert(jnum);\n" +
"location.href=String.format(\"" + url + "https://www.jb51.net/article/\",jnum);\n" +
"}\n"+
"}\n"+
"String.format = function() {\n"+
"if( arguments.length == 0 )\n"+
"return null; \n"+
"var str = arguments[0]; \n"+
"for(var i=1;i<arguments.length;i++) {\n"+
"var re = new RegExp('\\\\{' + (i-1) + '\\\\}','gm');\n"+
"str = str.replace(re, arguments[i]);\n"+
"}\n"+
"return str;\n"+
"}\n"+
"</script>\n";
return scriptstr;
}
}
最精简必要的几个参数传进去就能显示分页效果了:
复制代码 代码如下: