C#页码导航显示及算法实现代码
程序员文章站
2024-03-31 19:42:58
页码导航,先看显示效果: 算法要求: 1、页数小于等于1时不显示; 2、页数大于10时,自动缩短。 具体代码如下: 复制代码 代码如下: ///
页码导航,先看显示效果:
算法要求:
1、页数小于等于1时不显示;
2、页数大于10时,自动缩短。
具体代码如下:
/// <summary>
/// 获取分页导航
/// </summary>
/// <param name="intpageindex">页码</param>
/// <param name="intpagecount">页数</param>
/// <param name="strurl">链接</param>
/// <returns>html代码</returns>
private string gepagenavgation(int intpageindex, int intpagecount, string strurl)
{
stringbuilder sb = new stringbuilder("<div class=\"div_pagenavgation\">");
if (intpagecount > 1)
{
//页码显示
if (intpageindex == 1)
{
sb.append("<span class=\"disabled\"> < 上一页</span>");
}
else if (intpageindex > 1)
{
sb.append("<a href=\"" + strurl + (intpageindex - 1).tostring() + "\">< 上一页</a>");
}
bool dot1 = false, dot2 = false;
for (int i = 1; i <= intpagecount; i++)
{
if (i == intpageindex)
{
sb.append("<span class=\"current\">" + intpageindex.tostring() + "</span>");
continue;
}
if (i <= 3)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
if (intpageindex > 7)
{
if (!dot1)
{
sb.append("<span class=\"dotted\">...</span>");
dot1 = true;
}
}
if (i == intpageindex - 3)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
if (i == intpageindex - 2)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
if (i == intpageindex - 1)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
if (i == intpageindex + 1)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
if (i == intpageindex + 2)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
if (i == intpageindex + 3)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
if ((intpagecount - intpageindex) > 6 && i > intpageindex + 3)
{
if (!dot2)
{
sb.append("<span class=\"dotted\">...</span>");
dot2 = true;
}
}
if (i > intpagecount - 3)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
}
if (intpageindex == intpagecount)
{
sb.append("<span class=\"disabled\">下一页 ></span>");
}
else if (intpageindex + 1 <= intpagecount)
{
sb.append("<a href=\"" + strurl + (intpageindex + 1).tostring() + "\">下一页 ></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如下:
<div class="div_pagenavgation">
<a href="?m=log&page=11">< 上一页</a>
<a href="?m=log&page=1">1</a>
<a href="?m=log&page=2">2</a>
<a href="?m=log&page=3">3</a>
<span class="dotted">...</span>
<a href="?m=log&page=9">9</a>
<a href="?m=log&page=10">10</a>
<a href="?m=log&page=11">11</a>
<span class="current">12</span>
<a href="?m=log&page=13">13</a>
<a href="?m=log&page=14">14</a>
<a href="?m=log&page=15">15</a>
<span class="dotted">...</span>
<a href="?m=log&page=27">27</a>
<a href="?m=log&page=28">28</a>
<a href="?m=log&page=29">29</a>
<a href="?m=log&page=13">下一页 ></a>
</div>
说明:很久前赶工完成,基本能够实现要求,没有经过仔细优化,谁优化了再回复看看!
算法要求:
1、页数小于等于1时不显示;
2、页数大于10时,自动缩短。
具体代码如下:
复制代码 代码如下:
/// <summary>
/// 获取分页导航
/// </summary>
/// <param name="intpageindex">页码</param>
/// <param name="intpagecount">页数</param>
/// <param name="strurl">链接</param>
/// <returns>html代码</returns>
private string gepagenavgation(int intpageindex, int intpagecount, string strurl)
{
stringbuilder sb = new stringbuilder("<div class=\"div_pagenavgation\">");
if (intpagecount > 1)
{
//页码显示
if (intpageindex == 1)
{
sb.append("<span class=\"disabled\"> < 上一页</span>");
}
else if (intpageindex > 1)
{
sb.append("<a href=\"" + strurl + (intpageindex - 1).tostring() + "\">< 上一页</a>");
}
bool dot1 = false, dot2 = false;
for (int i = 1; i <= intpagecount; i++)
{
if (i == intpageindex)
{
sb.append("<span class=\"current\">" + intpageindex.tostring() + "</span>");
continue;
}
if (i <= 3)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
if (intpageindex > 7)
{
if (!dot1)
{
sb.append("<span class=\"dotted\">...</span>");
dot1 = true;
}
}
if (i == intpageindex - 3)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
if (i == intpageindex - 2)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
if (i == intpageindex - 1)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
if (i == intpageindex + 1)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
if (i == intpageindex + 2)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
if (i == intpageindex + 3)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
if ((intpagecount - intpageindex) > 6 && i > intpageindex + 3)
{
if (!dot2)
{
sb.append("<span class=\"dotted\">...</span>");
dot2 = true;
}
}
if (i > intpagecount - 3)
{
sb.append("<a href=\"" + strurl + i.tostring() + "\">" + i.tostring() + "</a>");
continue;
}
}
if (intpageindex == intpagecount)
{
sb.append("<span class=\"disabled\">下一页 ></span>");
}
else if (intpageindex + 1 <= intpagecount)
{
sb.append("<a href=\"" + strurl + (intpageindex + 1).tostring() + "\">下一页 ></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如下:
复制代码 代码如下:
<div class="div_pagenavgation">
<a href="?m=log&page=11">< 上一页</a>
<a href="?m=log&page=1">1</a>
<a href="?m=log&page=2">2</a>
<a href="?m=log&page=3">3</a>
<span class="dotted">...</span>
<a href="?m=log&page=9">9</a>
<a href="?m=log&page=10">10</a>
<a href="?m=log&page=11">11</a>
<span class="current">12</span>
<a href="?m=log&page=13">13</a>
<a href="?m=log&page=14">14</a>
<a href="?m=log&page=15">15</a>
<span class="dotted">...</span>
<a href="?m=log&page=27">27</a>
<a href="?m=log&page=28">28</a>
<a href="?m=log&page=29">29</a>
<a href="?m=log&page=13">下一页 ></a>
</div>
说明:很久前赶工完成,基本能够实现要求,没有经过仔细优化,谁优化了再回复看看!