效果如下:
程序代码:
#region 分页代码
public int pPageSize = 5;
public int pPageIndex = 1;
public int pPageCount = 0;
/// <summary>
/// 获取分页HTML代码
/// </summary>
/// <param name="pPageCount"></param>
/// <param name="pPageIndex"></param>
/// <param name="pParames"></param>
/// <returns></returns>
public string GetPageHtml(int pPageCount, int pPageIndex, int pPageSize, string pPageName, string pParames)
{
if (pPageIndex <= 0)
{
pPageIndex = 1;
}
if (pPageSize <= 0)
{
pPageSize = 10;
}
StringBuilder strHtml = new StringBuilder();
strHtml.Append("<div class=\"nub_guide\">");
if (pPageIndex > 1)
{
strHtml.Append(string.Format("<a href=\"{0}?pageSize={1}&pageIndex={2}{3}\">首页</a>", pPageName
, pPageSize
, 1
, string.IsNullOrEmpty(pParames) ? "" : "&" + pParames));
}
if (pPageIndex > 1)
{
strHtml.Append(string.Format("<a href=\"{0}?pageSize={1}&pageIndex={2}{3}\">上一页</a>", pPageName
, pPageSize
, pPageIndex - 1
, string.IsNullOrEmpty(pParames) ? "" : "&" + pParames));
}
int i = pPageIndex; //起始页
int Count = pPageCount; //循环的页码
//设置分页起始页
if ((pPageIndex - 1) % 5 == 0)
{
i = pPageIndex;
}
else
{
i = pPageIndex - ((pPageIndex - 1) % 5);
}
if ((pPageIndex - 1) % 5 == 0 && (pPageCount - pPageIndex + 1) % 5 == 0)
{
Count = pPageIndex + 4;
}
else
{
Count = pPageIndex + ((pPageCount - pPageIndex + 1) % 5);
}
if (Count > pPageCount)
{
Count = pPageCount;
}
for (; i <= Count; i++)
{
//当前页
if (i == pPageIndex)
{
strHtml.Append(string.Format("<a href=\"#\" class=\"now\" >{0}</a>", i.ToString()));
}
else
{
//其他页
strHtml.Append(string.Format("<a href=\"{0}?pageSize={1}&pageIndex={2}{3}\">{4}</a>", pPageName
, pPageSize
, i
, string.IsNullOrEmpty(pParames) ? "" : "&" + pParames
, i.ToString()));
}
}
if (pPageIndex < pPageCount)
{
strHtml.Append(string.Format("<a href=\"{0}?pageSize={1}&pageIndex={2}{3}\">下一页</a>", pPageName
, pPageSize
, pPageIndex + 1
, string.IsNullOrEmpty(pParames) ? "" : "&" + pParames));
}
if (pPageIndex < pPageCount)
{
strHtml.Append(string.Format("<a href=\"{0}?pageSize={1}&pageIndex={2}{3}\">尾页</a>", pPageName
, pPageSize
, pPageCount
, string.IsNullOrEmpty(pParames) ? "" : "&" + pParames));
}
strHtml.Append("</div>");
return strHtml.ToString();
}
#endregion