/// <summary>
/// 输出翻页控件HTML
/// </summary>
/// <param name="recordsum">总数据条数</param>
/// <param name="pageSize">每页条数</param>
/// <param name="pageIndex">当前页数</param>
/// <param name="pageFormat">分页字符串</param>
/// <param name="ifJavascript">是否为js分页</param>
/// <returns>翻页控件HTML</returns>
public static string GetPagerStr(int recordsum, int pageSize, int pageIndex, string pageFormat, bool ifJavascript = false)
{
string pagestr = "";
//如果总条目小于pageSize,那么什么都不输出
if (recordsum <= pageSize)
{
return "";
}
if (ifJavascript)//是js分页,就使用onclick
{
pageFormat = "javascript:;\" οnclick=\"" + pageFormat;
}
int pagesum = recordsum % pageSize == 0 ? recordsum / pageSize : recordsum / pageSize + 1;//共多少页
if (pageIndex > pagesum)
pageIndex = 1;
string formstr = "<div class=\"pager\">";
if (pageIndex > 1)
{
formstr += string.Format("<a href=\"" + pageFormat + "\">上一页</a>", (pageIndex - 1));
}
else
{
formstr += string.Format("<span class='pagerpre'>上一页</span>");
}
int count = pageIndex + 3 > pagesum ? pagesum : pageIndex + 3;
if (count - 7 > 0)
{
formstr += string.Format("<a href=\"" + pageFormat + "\">...</a>", count - 7);
}
for (int i = count - 7 > 0 ? count - 7 : 0; i < count; i++)
{
if (i + 1 != pageIndex)
{
formstr += string.Format("<a href=\"" + pageFormat + "\">" + (i + 1) + "</a>", (i + 1));
}
else
{
formstr += "<span>" + (i + 1) + "</span>";
}
}
if (count < pagesum)
{
formstr += string.Format("<a href=\"" + pageFormat + "\">...</a>", count + 1);
}
if (pageIndex < pagesum) formstr += string.Format("<a href=\"" + pageFormat + "\">下一页</a>", (pageIndex + 1));
else
{
formstr += string.Format("<span class='pagernext'>下一页</span>", (pageIndex + 1));
}
//if (recordsum > 0) formstr += "<li>共{4}页</li>";
formstr += "</div>";
pagestr = string.Format(formstr, pageIndex - 1, pageIndex + 1, pageIndex, recordsum, pagesum, recordsum);
return pagestr;
}
web 分页方法
最新推荐文章于 2024-09-18 17:50:21 发布