前台的分页
public static string divClass = "<div class='viciao'>";
public static string spanClass = "<span class='disabled'>";
/// <summary> /// 分页 /// </summary> /// <param name="png">当前页码</param> /// <param name="sumNum">总的页数</param> /// <param name="pagesize">每页的容量</param> /// <param name="fun">分页的js函数</param> /// <param name="divClass">分页的格式 </param> /// <returns></returns> public static string PrePager(int png, int sumNum, int pagesize, string fun, params string[] par) { string result = ""; int sum = sumNum / pagesize + (sumNum % pagesize == 0 ? 0 : 1);//总的页数 #region int start = 1;//开始的页码值 int end = 10;//页码最大的值 if (png + 5 >= sum) { end = sum; if (sum > 9) { start = sum - 9; } else { start = 1; } } if (png + 5 < sum) { end = sum; if (sum > 9) { start = sum - 9; } else { start = 1; } } if (png - 5 > 0) { if (sum > (png + 5)) { start = png - 5; end = png + 5; } else { end = sum; start = (sum - 10 > 0) ? (sum - 10) : 1; } } if (png - 5 <= 0) { start = 1; if (sum > 10) { end = 10; } else { end = sum; } } #endregion //函数参数 string parm = string.Empty; if (par != null && par.Count() > 0) {//参数的字符串 for (int i = 0; i < par.Length; i++) { parm += ",'" + par[i] + "'"; } } for (int i = start; i <= end; i++) { if (i == png) { result += "<strong>" + i + "</strong>"; } else { result += "<a href=\"Javascript:" + fun + "(" + i + parm + ")\">" + i + "</a>"; } } string nextPage = ""; if (png < sum) { nextPage = "<a href=\"Javascript:" + fun + "(" + (png + 1) + parm + ")\">下一页> </a>"; } else { nextPage = "<a href=\"Javascript:" + fun + "(" + sum + parm + ")\">下一页> </a>"; } string prePage = ""; if (png == 1) { prePage = "<a href=\"Javascript:" + fun + "(" + 1 + parm + ")\"> <上一页</a>"; } else { prePage = "<a href=\"Javascript:" + fun + "(" + (png - 1) + parm + ")\"> <上一页</a>"; } string first = divClass + "<a href=\"Javascript:" + fun + "(" + 1 + parm + ")\">首页</a>"; string endPage = "<a href=\"Javascript:" + fun + "(" + sum + parm + ")\">末页</a>"; result += nextPage + endPage + " 总页数 " + sum; return first + spanClass + prePage + result + "</div>"; }
使用
//control中
ViewData["page"] = Commen.CommenFunction.PrePager(pn.Value, sum, num.Value, "mingjiaCreatePigeonPage", new string[] { id.ToString() });
//页面
js中
function mingjiaCreatePigeonPage(pn)
{}
<%=viewdata["page"]%>
//后台的分页
/// <summary>
/// 分页
/// </summary>
/// <param name="png">当前页码</param>
/// <param name="par">参数字符串 id=0385023495</param>
/// <param name="sumNum">总的数据条数</param>
/// <param name="path">当为空时,默认为前台,</param>
public static string Pager(int png, int sumNum, int pagesize, string path, string controller, string action, params string[] par)
{
string result = "";
string pars = "";
int sum = sumNum / pagesize + (sumNum % pagesize == 0 ? 0 : 1);//总的页数
#region
int start = 1;//开始的页码值
int end = 10;//页码最大的值
if (png + 5 >= sum)
{
end = sum;
if (sum > 9)
{
start = sum - 9;
}
else
{
start = 1;
}
}
if (png + 5 < sum)
{
end = sum;
if (sum > 9)
{
start = sum - 9;
}
else
{
start = 1;
}
}
if (png - 5 > 0)
{
if (sum > (png + 5))
{
start = png - 5;
end = png + 5;
}
else
{
end = sum;
start = (sum - 10 > 0) ? (sum - 10) : 1;
}
}
if (png - 5 <= 0)
{
start = 1;
if (sum > 10)
{
end = 10;
}
else
{
end = sum;
}
}
#endregion
if (par != null && par.Count() > 0)
{//参数的字符串
for (int i = 0; i < par.Length; i++)
{
pars += "&" + par[i];
}
}
for (int i = start; i <= end; i++)
{
if (i == png)
{
result += "<strong>" + i + "</strong>";
}
else
{
result += " <a href=\"/" + path + controller + "/" + action + "?pn=" + i + pars + "\">" + i + "</a>";
}
}
string nextPage = "";
if (png < sum)
{
nextPage = " <a href=\"/" + path + controller + "/" + action + "?pn=" + (png + 1) + pars + "\">下一页</a>";
}
else
{
nextPage = " <a href=\"/" + path + controller + "/" + action + "?pn=" + sum + pars + "\">下一页</a>";
}
string prePage = "";
if (png == 1)
{
prePage = " <a href=\"/" + path + controller + "/" + action + "?pn=" + 1 + pars + "\">上一页</a>";
}
else
{
prePage = " <a href=\"/" + path + controller + "/" + action + "?pn=" + (png - 1) + pars + "\"> 上一页</a>";
}
string first = " <a href=\"/" + path + controller + "/" + action + "?pn=" + 1 + pars + "\">首页</a>";
string endPage = " <a href=\"/" + path + controller + "/" + action + "?pn=" + sum + pars + "\">末页</a>";
result += nextPage + endPage + " 总页数 " + sum;
return first + prePage + result;
}