Asp. NET paging component learning and using--teaching article (source code)
Last Update:2017-02-28
Source: Internet
Author: User
asp.net| Pagination | source code using System;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.ComponentModel;
Using System.Text;
Namespace Nslzhpages
{
public class LzhPages:System.Web.UI.WebControls.WebControl
{
private int _count;//The number of record bars displayed per page
private int _currentpage;//Current page
private int _allcount;//All of the record bars
private int _showpages;//Display pages
The following script is used to enter page numbers from a text box
Private Const string scriptstring = "<script language= ' JavaScript ' >\n" +
"Function Go (Ctrl,max) \ n" +
"{\ n" +
"If (ctrl.value >= 1 && ctrl.value <= max) \ n" +
"{\ n" +
"Var url;\n" +
"Var index;\n" +
"url = location.href;\n" +
"index = Url.indexof ('? '); \ N "+
"if (index = = 1) \ n" +
"{\ n" +
"}\n" +
"Else\n" +
"{\ n" +
"url = url.substring (0,index); \ n" +
"}\n" +
"Location.href = URL + '? currentpage= ' + ctrl.value;\n" +
"}\n" +
"Else\n" +
"{\ n" +
"Alert (' You must enter a number that matches the page requirement, the maximum is: ' + max '); \ n" +
"Return false;\n" +
"}\n" +
"}\n" +
"</script>\n";
[DefaultValue, Category ("Customer")]
public int Count
{
Set
{
if (value <= 0)
_count = 1;
Else
_count = value;
}
Get
{
return _count;
}
}
[DefaultValue (1), Category ("Customer")]
public int CurrentPage
{
Set
{
if (Value < 0)
_currentpage = 1;
Else
_currentpage = value;
}
Get
{
return _currentpage;
}
}
[DefaultValue (1), Category ("Customer")]
public int Allcount
{
Get
{
return _allcount;
}
Set
{
if (_allcount < 0)
throw new Exception ("record total number of bars cannot be negative");
Else
_allcount = value;
}
}
[DefaultValue (1), Category ("Customer")]
public int pages//Total pages
{
Get
{
if (this._allcount% This._count > 0)
return ((int) this._allcount/this._count) + 1;
Else
return ((int) this._allcount/this._count);
}
}
[DefaultValue (1), Category ("Customer")]
public int Showpages
{
Set
{
if (Value > 0)
_showpages = value;
Else
_showpages = 9;
}
Get
{
return _showpages;
}
}
protected override void Render (HtmlTextWriter writer)
{
Base. Render (writer);
String Leftinfo;
StringBuilder centerinfo = new StringBuilder ();
The page bar is divided into three parts, Leftinfo is the leftmost part, used to display the current page/total pages, the number of records displayed per page
Leftinfo = "page:" + this. Currentpage.tostring () + "/" + this. Pages.tostring () + "" + "per page" + this. Count.tostring () + "strip" + "total" + this. Allcount.tostring () + "strip";
The middle part is the paging section
int min;//minimum number of pages to display
int max;//Maximum number of pages to display
if (this. CurrentPage > this. Pages)//The current page must be less than the maximum page
{
This. CurrentPage = this. Pages;
}
if (this. CurrentPage% this. Showpages = = 0)//if exactly divisible
{
Min = this. CurrentPage + 1;
Max = this. CurrentPage + this. Showpages;
}
else if (this. CurrentPage% this. Showpages = = 1 && this. CurrentPage > this. Showpages)
{
min = ((int) this. Currentpage/this. Showpages)-1) * this. Showpages +1;
Max = this. CurrentPage-1;
}
Else
{
min = (int) this. Currentpage/this. Showpages) * this. Showpages + 1;
max = ((int) this. Currentpage/this. Showpages) + 1) * this. Showpages;
}
String numberstr = "";//loop Generate number sequence part
String Absurl;//url? The left part
Absurl = this. Context.Request.Url.ToString ();
if (Absurl.indexof ("?") = = 1)
{
}
Else
{
Absurl = absurl.substring (0,absurl.indexof ("?"));
}
for (int i = min; I <= max; i++)
{
if (i <= this. Pages)//is only displayed if it is not greater than the maximum page
{
if (this. CurrentPage = i)//if the current page, with italic and red display
{
Numberstr = Numberstr + "<a href=" + Absurl + "? currentpage=" + i.tostring () + ">" + "<i style= ' color:red ' >" + i.ToString () + "</I>" + "</a>" + "\ n";
}
Else
{
Numberstr = Numberstr + "<a href=" + Absurl + "? currentpage=" + i.tostring () + ">" + i.tostring () + "</a>" + "\ n ";
}
}
}
First page, previous page, next page, last page
String First,previous,next,last;
The Absurl + "currentpage=1";
/////////
if (this. CurrentPage = 1)
Previous = Absurl + "? currentpage=1";
Else
Previous = Absurl + "? currentpage=" + (this. CURRENTPAGE-1). ToString ();
/////////
if (this. CurrentPage = = this. Pages)
Next = Absurl + "? currentpage=" + this. Pages;
Else
Next = Absurl + "? currentpage=" + (this. CurrentPage + 1). ToString ();
/////////
Last = Absurl + "? currentpage=" + this. Pages;
Centerinfo.appendformat ("<font face= ' webdings ' style= ' font-size:14px ') ><a href={0}>7</a><a Href={1}>3</a></font>{2}<font face= ' webdings ' style= ' font-size:14px ' ><a href={3}>4< /a><a href={4}>8</a></font> ", first,previous,numberstr,next,last);
StringBuilder sb = new StringBuilder ();//html string
Sb. AppendFormat ("<table style = ' font-size:12px ' border= ' 0 ' cellpadding= ' 0 ' cellspacing= ' 0 ' width= ' 100% ' > \ n" +
"<tr>\n" +
"<td width= ' 25% ' align= ' left ' >{0}</td>\n" +
"<td width= ' 61% ' align= ' right ' >{1}</td>\n" +
"<td width= ' 14% ' align= ' right ' ><input type= ' text ' name= ' T1 '" size= ' 4 ' style= ' Border-bottom:solid ' 1pt gray; Border-top:solid 1pt Gray; Border-left:solid 1pt gray;border-right:solid 1pt Gray; ' > \ <input type= ' button ' name= ' B1 ' size= ' 6 ' value=go style= ' border-bottom:solid 1pt gray;border-top:solid 1pt Gray ; Border-left:solid 1pt gray;border-right:solid 1pt Gray; ' ></td>\n ' +
"</tr>\n" +
"</table>", Leftinfo,
Centerinfo.tostring (),
This. Pages);
Writer. Write (sb.) ToString ());
}
protected override void OnPreRender (EventArgs e)
{
Base. OnPreRender (e);
if (! Page.isclientscriptblockregistered ("WEREW-332DFAF-FDAFDSFDSAFD"))
{
Page.registerclientscriptblock ("Werew-332dfaf-fdafdsfdsafd", scriptstring);
}
}
}
}