Page Control source code | PAGE control source code

Source: Internet
Author: User
Page Control source code | PAGE control source code

[Assembly: TagPrefix ("WebPages", "WebPages")] // XXX is the namespace of the custom control, and YYY is the name you want.
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Text;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Text. RegularExpressions;
Using System. Collections;
Namespace WebPages
{
[DefaultProperty ("Text")]
[ToolboxData ("<{0}: Pages runat = \" server \ "> </{0}: Pages>")]
Public class Pages: WebControl
{
Private int _ CuttentPage;
Private int _ NumCount;
Private int _ PageSize = 1;
Private string _ Url = "";
Private string _ RewriteFormat = "";
Private string _ RewriteRegex = "";
Private Dictionary <string, string> _ regs;
Public int CurrentPage
{
Set
{
If (Regex. IsMatch (Convert. ToString (value), "^ \ d + $ "))
{
This. _ CuttentPage = Convert. ToInt32 (value );
}
Else
{
This. _ CuttentPage = 0;
}
}
Get
{
Return this. _ CuttentPage;
}
}
Public int NumCount
{
Set
{
If (Regex. IsMatch (Convert. ToString (value), "^ \ d + $ "))
{
This. _ NumCount = Convert. ToInt32 (value );
}
Else
{
This. _ NumCount = 0;
}
}
Get
{
Return this. _ NumCount;
}
}
Public int PageSize
{
Set
{
If (Regex. IsMatch (Convert. ToString (value), "^ \ d + $ "))
{
This. _ PageSize = Convert. ToInt32 (value );
}
Else
{
This. _ PageSize = 1;
}
}
Get
{
Return this. _ PageSize;
}
}
// Address
Public string Url
{
Set
{
_ Url = value;
}
Get
{
If (string. IsNullOrEmpty (_ Url ))
{
Try
{
_ Url = this. Parent. Page. Request. Url. ToString ();
}
Catch
{

_ Url = "";
}

}
Return this. _ Url;
}
}
Public string RewriteFormat
{
Set {this. _ RewriteFormat = value ;}
Get {return this. _ RewriteFormat ;}
}
Public string RewriteRegex
{
Set {_ RewriteRegex = value ;}
Get {return this. _ RewriteRegex ;}
}
Public Dictionary <string, string> Regs
{
Set {
This. _ regs = value;
}
Get {
If (_ regs = null ){
_ Regs = new Dictionary <string, string> ();
}
Return _ regs;
}
}

[Bindable (true)]
[Category ("Appearance")]
[DefaultValue ("")]
[Localizable (true)]
Public string Text
{
Get
{
String s = (String) ViewState ["Text"];
Return (s = null )? String. Empty: s );
}

Set
{
ViewState ["Text"] = value;
}
}

Protected string getUrl (string url)
{
MatchCollection mats = Regex. Matches (url ,"(? <First> [\ w] *) = (? <Two> [^ &] * [\ u4e00-\ u9fa5] + [^ &] *) ", RegexOptions. IgnoreCase | RegexOptions. Multiline );
Foreach (Match mat in mats)
{

Url = url. replace (mat. groups ["first"]. value + "=" + mat. groups ["two"]. value, mat. groups ["first"]. value + "=" + HttpUtility. urlEncodeUnicode (mat. groups ["two"]. value. trim ()));
}
Return url;
}
/// <Summary>
/// The Bottom page of the current page displayed based on the current page number
/// </Summary>
/// <Param name = "current"> </param>
/// <Param name = "size"> </param>
/// <Param name = "zong"> </param>
/// <Returns> </returns>
Protected string GetPage (int current, int size, int zong)
{
String url = this. Url;
Url = getUrl (url );
String urltemp = "";
Urltemp = url. IndexOf ("page =")> = 0? (Regex. replace (url, "page = ([^ &] *)", "page = {0}", RegexOptions. multiline | RegexOptions. singleline): (url. indexOf ("? ")> = 0? (Url + "& page = {0}"): (url + "? Page = {0 }"));
If (this. RewriteFormat. Length> 0)
{
Regex reg = new Regex (RewriteRegex, RegexOptions. IgnoreCase );
If (reg. IsMatch (urltemp ))
{
Urltemp = reg. Replace (urltemp, RewriteFormat );
}
}
Int pagecount = zong % size = 0? (Zong/size): (zong/size + 1 );
Int num = current/10;

System. Text. StringBuilder sball = new System. Text. StringBuilder ("");
Int xian = current/10 + 1;

Int before = current % 10 = 0? (Current/10): (current/10 );
Sball. AppendFormat ("& nbsp; <a href = '" + string. Format (urltemp, current-1 <1? ("1"): (current-1). ToString () + "'> previous page </a> & nbsp ;");
For (int I = (before * 10-5)> 0? (Before * 10-5): (1); I <= (before * 10 = 0? (1): (before * 10-1); I ++)
{
If (before * 10-5) <0)
Continue;
Sball. appendFormat ("& nbsp; <a href = '" + string. format (urltemp, (I + 1 ). toString () + "'> {0} </a>", (I + 1 ). toString ());
Sball. Append ("& nbsp ;");
}
For (int I = (xian-1) * 10> pagecount? (Pagecount): (xian-1) * 10); I <(xian * 10 <pagecount? (Xian * 10): (pagecount); I ++)
{

If (I = 0)
Sball. appendFormat ("<a href = '" + string. format (urltemp, (I + 1 ). toString () + "'> "). append (I + 1 )). append ("</a> ");

Else
Sball. append ("& nbsp; <a href = '" + string. format (urltemp, (I + 1 ). toString () + "'> "). append (I + 1 )). append ("</a> ");

}
Sball. Append ("& nbsp; <a href = '" + string. Format (urltemp, (current + 1> pagecount? (Pagecount. ToString (): (current + 1). ToString () + "'> next page </a> ");
Sball. append ("& nbsp; Total "). append (zong. toString ()). append ("Entry & nbsp; current Entry & nbsp ;"). append (current ). toString ()). append ("& nbsp; page "). append ("& nbsp; Total & nbsp ;"). append (pagecount ). append ("& nbsp; page ");
Return sball. ToString ();

}

Protected override void RenderContents (HtmlTextWriter output)
{
String tempData = "<table border = \" 0 \ "width = \" 100% \ "height = \" 30px \ "style = \" margin: 0px \ "> <tr> <td style = \" width: 89% \ ">{0} </td> <td style = \" width: 30% \ "> <input type = \" text \ "id = \" Pages1_TbPageGo \ "onKeyUp = \" this. value = this. value. replace (/[^ \ d] +/, '') \" style = \ "width: 33px \ "/> <input id = \" BtGo \ "style = \" width: 34px \ "type = \" button \ "value = \" GO \ "onClick = \" document. location. href = document. location. href . IndexOf ('page = ')> = 0? (Document. location. href. replace (/page = ([^ &] *)/, 'page = '+ document. getElementById ('pages1 _ TbPageGo '). value) :( document. location. href. indexOf ('? ')> = 0? (Document. location. href + '& page =' + document. getElementById ('pages1 _ TbPageGo '). value) :( document. location. href + '? Page = '+ document. getElementById ('pages1 _ TbPageGo'). value) \ "/> </td> </tr> </table> ";
Output. Write (string. Format (tempData, GetPage (CurrentPage, PageSize, NumCount )));
}
}
}

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.