页码导航,先看显示效果:
算法要求:
1、页数小于等于1时不显示;
2、页数大于10时,自动缩短。
具体代码如下:
复制代码 代码如下:
/// <summary>
/// 获取分页导航
/// </summary>
/// <param>页码</param>
/// <param>页数</param>
/// <param>链接</param>
/// <returns>HTML代码</returns>
private string GePageNavgation(int intPageIndex, int intPageCount, string strUrl)
{
StringBuilder sb = new StringBuilder("<div class=https://www.jb51.net/article/\"div_pagenavgation\">");
if (intPageCount > 1)
{
//页码显示
if (intPageIndex == 1)
{
sb.Append("<span class=https://www.jb51.net/article/\"disabled\"> < 上一页</span>");
}
else if (intPageIndex > 1)
{
sb.Append("<a href=https://www.jb51.net/article/\"" + strUrl + (intPageIndex - 1).ToString() + "https://www.jb51.net/article/\">< 上一页</a>");
}
bool Dot1 = false, Dot2 = false;
for (int i = 1; i <= intPageCount; i++)
{
if (i == intPageIndex)
{
sb.Append("<span class=https://www.jb51.net/article/\"current\">" + intPageIndex.ToString() + "</span>");
continue;
}
if (i <= 3)
{
sb.Append("<a href=https://www.jb51.net/article/\"" + strUrl + i.ToString() + "https://www.jb51.net/article/\">" + i.ToString() + "</a>");
continue;
}
if (intPageIndex > 7)
{
if (!Dot1)
{
sb.Append("<span class=https://www.jb51.net/article/\"dotted\">...</span>");
Dot1 = true;
}
}
if (i == intPageIndex - 3)
{
sb.Append("<a href=https://www.jb51.net/article/\"" + strUrl + i.ToString() + "https://www.jb51.net/article/\">" + i.ToString() + "</a>");
continue;
}
if (i == intPageIndex - 2)
{
sb.Append("<a href=https://www.jb51.net/article/\"" + strUrl + i.ToString() + "https://www.jb51.net/article/\">" + i.ToString() + "</a>");
continue;
}
if (i == intPageIndex - 1)
{
sb.Append("<a href=https://www.jb51.net/article/\"" + strUrl + i.ToString() + "https://www.jb51.net/article/\">" + i.ToString() + "</a>");
continue;
}
if (i == intPageIndex + 1)
{
sb.Append("<a href=https://www.jb51.net/article/\"" + strUrl + i.ToString() + "https://www.jb51.net/article/\">" + i.ToString() + "</a>");
continue;
}
if (i == intPageIndex + 2)
{
sb.Append("<a href=https://www.jb51.net/article/\"" + strUrl + i.ToString() + "https://www.jb51.net/article/\">" + i.ToString() + "</a>");
continue;
}
if (i == intPageIndex + 3)
{
sb.Append("<a href=https://www.jb51.net/article/\"" + strUrl + i.ToString() + "https://www.jb51.net/article/\">" + i.ToString() + "</a>");
continue;
}
if ((intPageCount - intPageIndex) > 6 && i > intPageIndex + 3)
{
if (!Dot2)
{
sb.Append("<span class=https://www.jb51.net/article/\"dotted\">...</span>");
Dot2 = true;
}
}
if (i > intPageCount - 3)
{
sb.Append("<a href=https://www.jb51.net/article/\"" + strUrl + i.ToString() + "https://www.jb51.net/article/\">" + i.ToString() + "</a>");
continue;
}
}
if (intPageIndex == intPageCount)
{
sb.Append("<span class=https://www.jb51.net/article/\"disabled\">下一页 ></span>");
}
else if (intPageIndex + 1 <= intPageCount)
{
sb.Append("<a href=https://www.jb51.net/article/\"" + strUrl + (intPageIndex + 1).ToString() + "https://www.jb51.net/article/\">下一页 ></a>");
}
}
sb.Append("</div>");
return sb.ToString();
}
CSS样式:
复制代码 代码如下:
.div_pagenavgation{
margin-top:5px;
line-height:20px;
float:right;
}
.div_pagenavgation a{
float:left;
text-decoration:none;
color:#06F;
margin-left:5px;
padding-left:5px;
padding-right:5px;
height:20px;
text-align:center;
display:block;
border:1px #CCC solid;
}
.div_pagenavgation .dotted{
float:left;
}
.div_pagenavgation .current{
float:left;
margin-left:5px;
padding-left:5px;
padding-right:5px;
height:20px;
text-align:center;
display:block;
border:1px #CCC solid ;
background-color:#0080C0;
color:#FFF;
}
.div_pagenavgation .disabled{
float:left;
margin-left:5px;
padding-left:5px;
padding-right:5px;
height:20px;
text-align:center;
display:block;
border:1px #CCC solid;
color:#CCC;
}
产生的HTML如下:
复制代码 代码如下: