asp.net pagination Control Source code _ Practical Skills

Source: Internet
Author: User

Characteristics:
, support datagrid,datalist data display control, in theory, as long as there are datasource control can
, support URL way paging, url paging every page can be searched by search engines, and support URL rewrite
, control styles can be customized
, encapsulation complete, use a program just write a line of code

Copy Code code as follows:

/******************************************************************
* * FileName: Pager.cs
* * Founder: Yang Xiang Wu
* * Date: July 27, 2005
* * Modified Person:
* * Date:
* * Description:
**
**----------------------------------------------------------------
******************************************************************/

Using System;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.Web;
Using System.Web.UI;
Using System.Text.RegularExpressions;

Namespace Mycompany.controls
{
<summary>
Page-Paging controls
</summary>
public class Pager:label, INamingContainer
{
#region member variables and constructors
HttpContext context = HttpContext.Current;
Label Countlabel;
HyperLink Previousbutton;
HyperLink Nextbutton;
HyperLink Firstbutton;
HyperLink Lastbutton;
Hyperlink[] paginglinkbuttons;

public override ControlCollection Controls
{
Get
{
EnsureChildControls ();
Return base. Controls;
}
}

protected override void CreateChildControls ()
{
Controls.clear ();

Addcountlabel ();

Addpagebuttons ();

Addpreviousnexthyperlinks ();

Addfirstlasthyperlinks ();
}
#endregion

#region Rendering method
protected override void Render (HtmlTextWriter writer)
{
No controls are rendered when the total number of pages modified is less than or equal to 1
int totalpages = Calculatetotalpages ();
//
if (totalpages <= 1)
Return

AddAttributesToRender (writer);

Writer. AddAttribute (Htmltextwriterattribute.class, this. CssClass, false);

Rendercountlabel (writer);

Renderfirst (writer);

Renderprevious (writer);

Renderpagingbuttons (writer);

Rendernext (writer);

Renderlast (writer);
}

void Rendercountlabel (HtmlTextWriter writer)
{
Countlabel.rendercontrol (writer);

LiteralControl L = new LiteralControl ("");
L.rendercontrol (writer);
}

void Renderfirst (HtmlTextWriter writer)
{
Firstbutton.rendercontrol (writer);

LiteralControl L = new LiteralControl ("");
L.rendercontrol (writer);
}

void Renderlast (HtmlTextWriter writer)
{
Lastbutton.rendercontrol (writer);

LiteralControl L = new LiteralControl ("");
L.rendercontrol (writer);
}

void Renderprevious (HtmlTextWriter writer)
{
Previousbutton.rendercontrol (writer);

LiteralControl L = new LiteralControl ("");
L.rendercontrol (writer);
}

void Rendernext (HtmlTextWriter writer)
{
Nextbutton.rendercontrol (writer);

LiteralControl L = new LiteralControl ("");
L.rendercontrol (writer);
}

private void Renderbuttonrange (int start, int end, HtmlTextWriter writer)
{
for (int i = start; i < end; i++)
{
if (PageIndex = i)
{
Literal L = new Literal ();
L.text = "<font color =red>" + (i + 1). ToString () + "</font>";

L.rendercontrol (writer);
}
Else
{
Paginglinkbuttons.rendercontrol (writer);
}
if (I < (end-1))
Writer. Write ("");
}

LiteralControl L1 = new LiteralControl ("");
L1. RenderControl (writer);
}

private void Renderpagingbuttons (HtmlTextWriter writer)
{
int totalpages;

TotalPages = Calculatetotalpages ();

if (totalpages <= 10)
{
Renderbuttonrange (0, TotalPages, writer);
}
Else
{
int lowerbound = (PageIndex-4);
int Upperbount = (PageIndex + 6);

if (lowerbound <= 0)
lowerbound = 0;

if (PageIndex <= 4)
Renderbuttonrange (0, writer);

else if (PageIndex < (totalPages-5))
Renderbuttonrange (Lowerbound, (PageIndex + 6), writer);

else if (PageIndex >= (totalPages-5))
Renderbuttonrange ((totalPages-10), totalpages, writer);
}
}

#endregion

#region Control Tree Method
<summary>
Information labels
</summary>
void Addcountlabel ()
{
Countlabel = new Label ();
Countlabel.id = "Countlabel";
Countlabel.text = string. Format (text, Calculatetotalpages (). ToString ("N0"));

Controls.Add (Countlabel);
}

private void Addpagebuttons ()
{
Paginglinkbuttons = new Hyperlink[calculatetotalpages ()];

for (int i = 0; i < paginglinkbuttons.length; i++)
{
Paginglinkbuttons = new HyperLink ();
Paginglinkbuttons.enableviewstate = false;
Paginglinkbuttons.text = (i + 1). ToString ();
Paginglinkbuttons.id = i.ToString ();
Paginglinkbuttons.cssclass = Linkcss;
Paginglinkbuttons.navigateurl = gethrefstring (i);

Controls.Add (paginglinkbuttons);
}
}

<summary>
Home last
</summary>
void Addfirstlasthyperlinks ()
{
Firstbutton = new HyperLink ();
Firstbutton.id = "a";
Firstbutton.text = "Home page";
if (PageIndex!= 0 && calculatetotalpages () > 0)
{
Firstbutton.navigateurl = gethrefstring (0);
}
Else
{
firstbutton.enabled = false;
}

Controls.Add (Firstbutton);

Lastbutton = new HyperLink ();
Lastbutton.id = "Last";
Lastbutton.text = "Last page";
if (PageIndex!= calculatetotalpages ()-1)
{
Lastbutton.navigateurl = gethrefstring (Calculatetotalpages ()-1);
}
Else
{
lastbutton.enabled = false;
}
Controls.Add (Lastbutton);
}

<summary>
Previous page Next page
</summary>
void Addpreviousnexthyperlinks ()
{
Previousbutton = new HyperLink ();
Previousbutton.id = "Prev";
Previousbutton.text = "Previous page";
if (hasprevious)
{
Previousbutton.navigateurl = gethrefstring (PageIndex-1);
}
Else
{
previousbutton.enabled = false;
}
Controls.Add (Previousbutton);

Nextbutton = new HyperLink ();
Nextbutton.id = "Next";
Nextbutton.text = "Next page";
if (Hasnext)
{
Nextbutton.navigateurl = gethrefstring (PageIndex + 1);
}
Else
{
nextbutton.enabled = false;
}
Controls.Add (Nextbutton);
}
#endregion

#region Private Property
private bool Hasprevious
{
Get
{
if (PageIndex > 0)
return true;

return false;
}
}

private bool Hasnext
{
Get
{
if (PageIndex + 1 < calculatetotalpages ())
return true;

return false;
}
}
#endregion

#region Help methods and public properties

<summary>
Get a hyperlink string for a pager button
</summary>
<param name= "PageIndex" > page index corresponding to the paging button </param>
<returns> Pager button Hyperlink string </returns>
private string gethrefstring (int pageIndex)
{
string url = Page.Request.RawUrl;

if (Regex.IsMatch (URL, @ "(\d+). shtml", regexoptions.ignorecase))
{
url = regex.replace (URL, @ "(\d+). shtml", PageIndex + ". shtml");
}

if (Regex.IsMatch (URL, @ "pageindex= (\d+)", Regexoptions.ignorecase))
{
url = regex.replace (URL, @ "pageindex= (\d+)", "pageindex=" + pageindex.tostring ());
}

return Page.Server.UrlPathEncode (URL);
}

public int calculatetotalpages ()
{
int totalpagesavailable;

if (totalrecords = 0)
return 0;

totalpagesavailable = totalrecords/pagesize;

if ((totalrecords% PageSize) > 0)
totalpagesavailable++;

return totalpagesavailable;
}

<summary>
Current page
</summary>
public int PageIndex
{
Get
{
int _pageindex = 0;

if (! Page.IsPostBack) && (context. PageIndex > 0))
_pageindex = context. PageIndex;
Else
_pageindex = Convert.ToInt32 (viewstate["PageIndex"));

if (_pageindex < 0)
return 0;
Else
{
if (_pageindex > Calculatetotalpages ()-1)
Return Calculatetotalpages ()-1;
Else
return _pageindex;
}
}
Set
{
viewstate["PageIndex"] = value;
}
}

<summary>
Number of records per page
</summary>
public int PageSize
{
Get
{
int pageSize = Convert.ToInt32 (viewstate["pageSize"]);

if (pageSize = 0)
{
return 10;
}

return pageSize;
}
Set
{
viewstate["PageSize"] = value;
}

}

<summary>
Total number of records
</summary>
public int Totalrecords
{
Get
{
Return Convert.ToInt32 (viewstate["totalrecords"]);
}
Set
{
viewstate["totalrecords"] = value;
}
}

private string Text = "Total {0} pages";

private string linkcss = "";
<summary>
Link Style
</summary>
public string Linkcss
{
get {return linkcss;}
set {linkcss = value;}
}

private string textcss = "";
<summary>
Text Style
</summary>
public string Textcss
{
get {return textcss;}
set {textcss = value;}
}

public string TextFormat
{
Get
{
return text;
}
Set
{
Text = value;
}
}

#endregion
}
}

You only need to write this on the page as follows:
Copy Code code as follows:

<%@ control language= "C #"%>
<%@ Register tagprefix= "Anchor" namespace= "Mycompany.controls" assembly= "Mycompany.controls"%>
<anchor:pager id= "Pager1" runat= "Server" ></ANCHOR:PAGER>

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.