Refine a custom paging control

Source: Internet
Author: User

The previous article wrote a simple custom paging control that was not well written and could not customize the style of the control. Now perfect, hope and everyone to discuss together.

Now published on the Web page control is particularly large, and most of them are particularly strong, but the reason to choose their own writing, mainly because of their own writing can be based on their own requirements to design, the function of the function is free.

The control can customize the style and pass in parameters similar to other commonly used paging controls, record totals and number of pages, customize styles, and when the number of pages is particularly large, you can display the first few pages and the last few pages in the page Information Bar.

The specific code is as follows:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Text;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Namespace Pagercontrols
{
[Defaultproperty ("Text")]
[ToolBoxData ("<{0}:webcustomcontrolpager runat=server></{0}:webcustomcontrolpager>")]
public class Webcustomcontrolpager:webcontrol
{
private string _pageurl= "";
/**////<summary>
Current page address
</summary>
[Bindable (True)]
[CategoryAttribute ("appearance")]
[DefaultValue ("")]
[Localizable (true)]
[DescriptionAttribute ("current page address")]
Public virtual String Pageurl
{
Get
{
return this._pageurl;
}
Set
{
this. _pageurl = value;
}
}
private int _currentpageindex;
/**////<summary>
Current page Index
</summary>
[Bindable (True)]
[CategoryAttribute ("appearance")]
[Localizable (true)]
[DescriptionAttribute ("Current page index starts from 1")]
[DefaultValueAttribute (current page index)]
public virtual int CurrentPageIndex
{
Get
{
return this._currentpageindex;
}
Set
{
This._currentpageindex = value;
}
}
private int _iscustomstyle;
/**////<summary>
Whether to customize styles
0: not 1: Yes
</summary>
[Bindable (True)]
[CategoryAttribute ("appearance")]
[Localizable (true)]
[DescriptionAttribute ("Custom Style")]
[DefaultValueAttribute ("0")]
public virtual int Iscustomstyle
{
Get
{
return this._iscustomstyle;
}
Set
{
This._iscustomstyle = value;
}
}
private int _irecordcount=1;
/**////<summary>
Number of records
</summary>
[Bindable (True)]
[CategoryAttribute ("appearance")]
[Localizable (true)]
[DescriptionAttribute ("Number of records")]
[DefaultValueAttribute ("Number of records")]
public virtual int iRecordCount
{
Get
{
return this._irecordcount;
}
Set
{
This._irecordcount = value;
}
}
private int _irowscount=10;
/**////<summary>
Number of records per page
</summary>
[Bindable (True)]
[CategoryAttribute ("appearance")]
[Localizable (true)]
[DescriptionAttribute (Number of records per page)]
[DefaultValueAttribute (Number of records per page)]
public virtual int Irowscount
{
Get
{
return this._irowscount;
}
Set
{
This._irowscount = value;
}
}
private int _iprevcount=5;
/**////<summary>
Number of previous records
</summary>
[Bindable (True)]
[CategoryAttribute ("appearance")]
[Localizable (true)]
[DescriptionAttribute ("Number of previous records")]
[DefaultValueAttribute ("Number of previous records")]
public virtual int Iprevcount
{
Get
{
return this._iprevcount;
}
Set
{
This._iprevcount = value;
}
}
private int _inextcount=5;
/**////<summary>
Number of records in the latter section
</summary>
[Bindable (True)]
[CategoryAttribute ("appearance")]
[Localizable (true)]
[DescriptionAttribute ("Number of subsequent records")]
[DefaultValueAttribute ("Number of subsequent records")]
public virtual int Inextcount
{
Get
{
return this._inextcount;

}
Set
{
This._inextcount = value;
}
}
/**////<summary>
Get Default style sheet information
by Minjiang 08-3-24
</summary>
<returns></returns>
private String stylehtml ()
{
Page-style sheet information
String sstyle = "";
StringBuilder Strbstyle = new StringBuilder ();
Sstyle = "<style type =\" text/css\ ">";
Strbstyle.append (Sstyle);
Style content #region Style content
Sstyle = ". a4:link,.a4:visited,.a4:active {color: #207FC3; font-size:12px;text-decoration:none;}";
Strbstyle.append (Sstyle);
Sstyle = ". A4:hover{color: #ff6600; font-size:12px;text-decoration:none;}";
Strbstyle.append (Sstyle);
Sstyle = ". A5:link,.a5:visited,.a5:active{color: #ffffff; font-size:12px;text-decoration:none;}";
Strbstyle.append (Sstyle);
Sstyle = ". A5:hover {color: #ffffff; font-size:12px;text-decoration:none;}";
Strbstyle.append (Sstyle);
Sstyle = ". Survey_pagediv {float:left;width:950px;height:22px; margin-top:15px;} ";
Strbstyle.append (Sstyle);
Sstyle = ". Survey_pagediv. pagedivcenter {width:600px; margin:0 auto;} ";
Strbstyle.append (Sstyle);
Sstyle = ". Survey_pagediv. Page {float:left;width:auto;font-family:verdana, Arial, Helvetica, Sans-serif;}";
Strbstyle.append (Sstyle);
Sstyle = ". Survey_pagediv. Page. Select {float:left;height:16px;line-height:16px;padding:0 4px 0 4px;display:block;border:solid 1px #207FC3; margin:0 2px 0 2px;background-color: #207FC3;} ";
Strbstyle.append (Sstyle);
Sstyle = ". Survey_pagediv. Page. num {float:left;height:16px;line-height:16px;padding:0 4px 0 4px;display:block;border:solid 1px #207FC3; margin:0 2px 0 2px;} ";
Strbstyle.append (Sstyle);
Sstyle = ". Survey_pagediv. Page span{float:left;height:18px;line-height:18px;display:block;margin:0 4px 0 4px;} ";
Strbstyle.append (Sstyle);
Sstyle = ". Survey_pagediv_nodiv {float:left; width:230px; Height:auto; Border:solid 1px #C2E8C7; Background-color: #FBFFFB; padding:5px;} ";
Strbstyle.append (Sstyle);
Sstyle = ". survey_pagediv_nodiv1 {float:left; width:230px; height:155px; Border:solid 1px #FFD4E3; Background-color: #FFFCFE; padding:5px; Text-align:center; line-height:155px;} ";
Strbstyle.append (Sstyle);
#endregion
Sstyle = "</style>";
Strbstyle.append (Sstyle);
End of Style content
Sstyle = Strbstyle.tostring ();
return sstyle;
}
protected override void RenderContents (HtmlTextWriter output)
{
Page-style sheet information
String sstyle = "";
if (this. Iscustomstyle = 0)
{
Sstyle = this.stylehtml ();
}
Paging information
String spagerhtml = "";
Spagerhtml = this. Csgetpagerhtml (this. iRecordCount, this. Irowscount, This.pageurl, this. currentpageindex);
spagerhtml = Sstyle + spagerhtml;
Output. Write (spagerhtml);
}
/**////<summary>
Get paging information
by Minjiang 08-3-11
</summary>
<param name= "RecordCount" > Total Records </param>
<param name= "Irowscount" > per page record size </param>
<param name= "Pageurl" > page address </param>
<returns></returns>
public string csgetpagerhtml (int recordCount, int irowscount, string pageurl, int icurrentpageindex)
{
int intprevpagecount = this. Iprevcount;
int intnextpagecount = this. Inextcount;
If it is less than this number, the ellipsis is not displayed
int intdefaultcount = this. Inextcount +this. Iprevcount;
StringBuilder strb = new StringBuilder ();
string info = "";
info = "<div class=\" survey_pagediv\ "><div class=\" pagedivcenter\ "><div class=\" page\ ">";
Strb. Append (info);
Link Address
if (Pageurl.indexof ("page=")!=-1)
{
int pageIndex = Pageurl.indexof ("page=");
Pageurl = pageurl.substring (0, PageIndex + 5);
}
Else
{
If the page does not have any parameters, add it? with parameters
if (Pageurl.indexof ("?") = = 1)
{
Pageurl = "page=";
}
Else
{
If you just don't have the page parameter, add some arguments
Pageurl + = "&page=";
}
}
Total pages
int pagecount = 1;
if (RecordCount > 0)
{
if (recordCount% Irowscount = 0)
{PageCount = Recordcount/irowscount;}
Else
{
if (Recordcount>irowscount)
{
If the number of pages is greater than 1
PageCount = Recordcount/irowscount + 1;
}
Else
{
Do not display page-paging information
Return "";
}
}
}
if (PageCount < 1)
{
If only one page does not display the paging information
Return "";
}
int currentpage = Icurrentpageindex;
Int. TryParse (Scurrentpageindex, out currentpage);
if (CurrentPage < 1)
{
CurrentPage = 1;
}
Previous Page Index
int prevpage = 1;
PrevPage = currentPage-1;
if (PrevPage < 1)
{prevpage = 1;}
Next page Index
int nextPage = 1;
NextPage = currentpage + 1;
if (NextPage > PageCount)
{nextPage = 1;}
Index page at start
int startpageindex = currentpage;
Total number of pages to be counted
int pagetotalcount = pagecount-currentpage + 1;
if (currentpage!= 1)
{
Show previous page if current page is not first
info = "<span><a href=\" "+ Pageurl + prevpage.tostring () +" \ ">&lt;&lt; previous page </a></span&gt ;";
Strb. Append (info);
}
Back Page
info = "<span><a href=\" "+ Pageurl +" 1\ "> Home </a></span>";
Strb. Append (info);
string pageclass = "num A4";
The current page has a style of select A5
Do not display ellipses if the number of pages is less than or equal to 10 pages
if (PageCount <= intdefaultcount)
{
for (int kk = 1; KK <= PageCount; kk++)
{
Pageclass = "num A4";
if (KK = = currentpage)
{
Pageclass = "Select A5";
}
info = "<a href=\" + Pageurl + KK. ToString () + "\" class=\ "" + Pageclass + "\" > "+ KK. ToString () + "</a>";
Strb. Append (info);
}
Next page address
info = "<span><a href=\" "+ Pageurl + nextpage.tostring () +" \ "> next page &gt;&gt;</a></span&gt ;";
Strb. Append (info);
info = strb. ToString ();
return info;
}
Show 5 pages before ellipsis
if (Pagetotalcount <= intdefaultcount)
{
If the number of pages you want to count is less than the number of parts before the page number plus pagination
The difference between the number of pages to be counted and the number of parts before the page is paginated
The page bar always displays the number of page links before the number of pages before pagination
int idispersion = Intdefaultcount-pagetotalcount;
for (int k = currentpage-idispersion k <= pagecount; k++)
{
Pageclass = "num A4";
if (k = = currentpage)
{
Pageclass = "Select A5";
}
info = "<a href=\" "+ Pageurl + k.tostring () +" "class=\" "+ Pageclass +" \ ">" + k.tostring () + "</a>";
Strb. Append (info);
}
}
Else
{
for (int k = currentpage; K <= currentpage + intPrevPageCount-1; k++)
{
Pageclass = "num A4";
if (k = = currentpage)
{
Pageclass = "Select A5";
}
info = "<a href=\" "+ Pageurl + k.tostring () +" "class=\" "+ Pageclass +" \ ">" + k.tostring () + "</a>";
Strb. Append (info);
}
if (Pagetotalcount > (Intprevpagecount + intnextpagecount))
{
If the total number of pages to count is greater than the number of records before the ellipsis and the amount of the record after the ellipsis, the
info = "<span></span>";
Strb. Append (info);
}
Start index
int _inextsatrindex = pagecount-intnextpagecount+1;
for (int j = _inextsatrindex J <= PageCount; j + +)
{
Pageclass = "num A4";
if (j = = CurrentPage)
{
Pageclass = "Select A5";
}
info = "<a href=\" "+ Pageurl + j.tostring () +" "class=\" "+ Pageclass +" \ ">" + j.tostring () + "</a>";
Strb. Append (info);
}
}
Back to Last
info = "<span><a href=\" "+ pageurl +pagecount. ToString () + "\" > Last </a></span> ";
Strb. Append (info);
if (currentpage!= PageCount)
{
Display the next page if it is not the last page
info = "<span><a href=\" "+ Pageurl + nextpage.tostring () +" \ "> next page &gt;&gt;</a></span> ";
Strb. Append (info);
}
info = "</div></div></div>";
Strb. Append (info);
info = strb. ToString ();
return info;
}
}
}

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.