C # page number navigation display and algorithm implementation code _ practical skills

Source: Internet
Author: User
Tags prev
Page number navigation, first look at the display effect:

Algorithm Requirements
1, the page is less than or equal to 1 o'clock does not show;
2, the number of pages greater than 10 o'clock, automatically shortened.
The specific code is as follows:
Copy Code code as follows:

<summary>
Get page Navigation
</summary>
<param name= "Intpageindex" > page number </param>
<param name= "Intpagecount" > Page </param>
<param name= "strURL" > Links </param>
<returns>html Code </returns>
private string gepagenavgation (int intpageindex, int intpagecount, string strurl)
{
StringBuilder sb = new StringBuilder ("<div class=\" div_pagenavgation\ ">");
if (Intpagecount > 1)
{
Page number Display
if (Intpageindex = 1)
{
Sb. Append ("<span class=\" disabled\ "> < prev </span>");
}
else if (Intpageindex > 1)
{
Sb. Append ("<a href=\" "+ strURL + (intPageIndex-1). ToString () + "\" >< prev </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\ "> Next page ></span>");
}
else if (Intpageindex + 1 <= intpagecount)
{
Sb. Append ("<a href=\" "+ strURL + (Intpageindex + 1). ToString () + "\" > Next page ></a> ");
}
}
Sb. Append ("</div>");
Return SB. ToString ();
}

CSS style:
Copy Code code as follows:

. 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;
}

The resulting HTML is as follows:
Copy Code code as follows:

<div class= "Div_pagenavgation" >
<a href= "? m=log&page=11" >< prev </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" > Next page ></a>
</div>

Description: A long time before the completion of the work, the basic ability to achieve the requirements, without careful optimization, who optimized the reply to see!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.