頁(yè)碼導(dǎo)航,先看顯示效果:
算法要求:
1、頁(yè)數(shù)小于等于1時(shí)不顯示;
2、頁(yè)數(shù)大于10時(shí),自動(dòng)縮短。
具體代碼如下:
復(fù)制代碼 代碼如下:
/// summary>
/// 獲取分頁(yè)導(dǎo)航
/// /summary>
/// param name="intPageIndex">頁(yè)碼/param>
/// param name="intPageCount">頁(yè)數(shù)/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)
{
//頁(yè)碼顯示
if (intPageIndex == 1)
{
sb.Append("span class=\"disabled\"> 上一頁(yè)/span>");
}
else if (intPageIndex > 1)
{
sb.Append("a href=\"" + strUrl + (intPageIndex - 1).ToString() + "\"> 上一頁(yè)/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\">下一頁(yè) >/span>");
}
else if (intPageIndex + 1 = intPageCount)
{
sb.Append("a href=\"" + strUrl + (intPageIndex + 1).ToString() + "\">下一頁(yè) >/a>");
}
}
sb.Append("/div>");
return sb.ToString();
}
CSS樣式:
復(fù)制代碼 代碼如下:
.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;
}
產(chǎn)生的HTML如下:
復(fù)制代碼 代碼如下:
div class="div_pagenavgation">
a href="?m=logpage=11"> 上一頁(yè)/a>
a href="?m=logpage=1">1/a>
a href="?m=logpage=2">2/a>
a href="?m=logpage=3">3/a>
span class="dotted">.../span>
a href="?m=logpage=9">9/a>
a href="?m=logpage=10">10/a>
a href="?m=logpage=11">11/a>
span class="current">12/span>
a href="?m=logpage=13">13/a>
a href="?m=logpage=14">14/a>
a href="?m=logpage=15">15/a>
span class="dotted">.../span>
a href="?m=logpage=27">27/a>
a href="?m=logpage=28">28/a>
a href="?m=logpage=29">29/a>
a href="?m=logpage=13">下一頁(yè) >/a>
/div>
說(shuō)明:很久前趕工完成,基本能夠?qū)崿F(xiàn)要求,沒(méi)有經(jīng)過(guò)仔細(xì)優(yōu)化,誰(shuí)優(yōu)化了再回復(fù)看看!
您可能感興趣的文章:- c# TreeView添加右鍵快鍵菜單有兩種方法
- 基于C#實(shí)現(xiàn)的仿windows左側(cè)伸縮菜單效果
- C#實(shí)現(xiàn)Winform動(dòng)態(tài)添加菜單的方法
- C#自定義控件添加右鍵菜單的方法
- 使用c#開(kāi)發(fā)公眾平臺(tái)自定義菜單功能
- asp.net(C#)生成無(wú)限級(jí)別菜單
- WPF實(shí)現(xiàn)半圓形導(dǎo)航菜單