I recently sorted out the asp.net project code, but I had no habit of recording notes before. I want to use this opportunity to archive some of the Code commonly used in the project. Today I want to sort out the most common code and paging controls.
There are many paging controls on the Internet, which are also very useful, but I developed this paging control myself and I think it is OK. The page control is roughly as follows:
1. Create a new user control in the project, named as the PageShow-1.
Figure-1
2. Add an asp: Literal control to the open page. The Code is as follows:
<% @ Control Language = "C #" AutoEventWireup = "true" CodeBehind = "PageShow. ascx. cs" Inherits = "Web. control. PageShow" %>
<Asp: Literal ID = "ltlPager" runat = "server"> </asp: Literal>
3. Compile the background code to implement the paging function, and write some code comments in the code.
Using System;
Using System. Collections;
Using System. Configuration;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Text;
Using System. Text. RegularExpressions;
Namespace Web. control
{
Public partial class PageShow: System. Web. UI. UserControl
{
Private int pageSize; // page size
Private int recordCount; // The total number of records.
Private int page; // the current page number.
Private int pageCount; // the total number of pages.
Private string url; // the address of the current page.
Private int width; // control width
Private string cssClass; // control css Class Name
Private string align; // alignment Mode
Private string pageHtml;
Private bool? EnablePageInfo; // whether the page information is displayed
Private bool? EnableJumpPage; // whether to display the jump Information
Protected void Page_Load (object sender, EventArgs e)
{
PageCount = PageCount;
Page = CurrentPage;
Url = Url;
String filename = System. IO. Path. GetFileName (HttpContext. Current. Request. Path );
Url = filename + url;
Width = Width;
PageHtml + = (width = 0 )? "<Div class = \" "+ CssClas +" \ ">": "<div class = \" "+ CssClas +" \ "style = \" width: "+ width +" px \ "> ";
If (EnablePageInfo = true) AppendPageInfo ();
PageHtml + = (page <= 1 )? "<Span class = \" disabled \ "> previous page </span>": "<a href = \" "+ url. replace ("page =" + page, "page =" + (page-1 )) + "\" title = \ "Previous Page \" class = \ "pagerPrev \"> previous page </a> ";
PageHtml + = (page <= 1 )? "<Span class = \" current \ ">" + page + "</span>": "<a href = \" "+ url. replace ("page =" + page, "page = 1") + "\" title = \ "Jump to page 1st \"> 1 </a> ";
Int start = 2;
Int end = (pageCount <10 )? PageCount: 10;
If (page> 8)
{
PageHtml + = "...";
Start = page-4;
Int end1 = page + 5;
End = (pageCount <end1 )? PageCount: end1;
}
For (int I = start; I <end; I ++)
{
PageHtml + = (I = page )? "<Span class = \" current \ ">" + I + "</span>": "<a href = \" "+ url. replace ("page =" + page, "page =" + I) + "\" title = \ "Jump to the" + I + "Page \"> "+ I +" </a> ";
}
PageHtml + = (page + 5 <pageCount )? "...":"";
PageHtml + = (page = pageCount )? "<Span class = \" current \ ">" + page + "</span>": "<a href = \" "+ url. replace ("page =" + page, "page =" + pageCount) + "\" title = \ "Jump to section" + pageCount + "Page \"> "+ pageCount +" </a> ";
PageHtml + = (page = pageCount )? "<Span class = \" disabled \ "> next page </span>": "<a href = \" "+ url. replace ("page =" + page, "page =" + (page + 1 )) + "\" title = \ "Next Page \" class = \ "pagerNext \"> next page </a> ";
If (EnableJumpPage = true) AppendJumpPageScript ();
PageHtml + = "</div> ";
LtlPager. Text = pageHtml;
}
Private void AppendJumpPageScript ()
{
PageHtml + = "& nbsp; <span class = 'jumpage'> jump to the <input id = \" txtPagebarJumpPage \ "class = \" pagebarTextBox \ "onfocus = \" this. value = ''\" type = \ "text \" value = \ "" + page + "\" style = \ "width: 20px; \"/> page & nbsp; <input type = \ "button \" class = \ "pagerButton \" onclick = \ "jumpPage (); \ "value = \" OK \ "title = \" Jump to specified Page \ "/> </span>" + System. environment. newLine;
PageHtml + = "<script type = \" text/javascript \ "> \ n ";
PageHtml + = "// <! [CDATA [\ n ";
PageHtml + = "function jumpPage () {\ n ";
PageHtml + = "var url; \ r \ n ";
PageHtml + = "var pageNo = document. getElementById ('txtpagebarjumppage'). value; \ n ";
PageHtml + = "if (document. URL. indexOf ('page = ')> 0) \ r \ n ";
PageHtml + = "url = document. URL. replace ('page =" + page + "', 'page =' + pageNo); \ n ";
PageHtml + = "else \ n ";
PageHtml + = "url = document. URL. indexOf ('? ')> 0? Document. URL + '& amp; page =' + pageNo: document. URL + '? Page = '+ pageNo; \ n ";
PageHtml + = "location. href = url; \ n ";
PageHtml + = "} \ r \ n ";
PageHtml + = "//]> \ r \ n ";
PageHtml + = "</script> \ r \ n ";
}
Private void AppendPageInfo ()
{
PageHtml + = "<span class = \" pageInfo \ "> page" + CurrentPage + "/" + PageCount + "page" + System. environment. newLine + "</span> ";
}
# Region protected attributes
/// <Summary>
/// Calculate the page number
/// </Summary>
Private int PageCount
{
Get
{
Return (recordCount = 0 )? 1: (int) (Math. Ceiling (double) recordCount/pageSize ));
}
}
/// <Summary>
/// Current page number
/// </Summary>
Private int CurrentPage
{
Get
{
String query = Request. Url. Query;
String pageString = "page = 1 ";
Regex regex = new Regex (@ "(page = \ d *)", RegexOptions. IgnoreCase | RegexOptions. Compiled );
Match match = regex. Match (query );
If (match. Success)
{
PageString = match. Groups [1]. ToString ();
}
If (pageString. IndexOf ("=")>-1)
{
String [] arrPage = pageString. Split (new Char [] {'= '});
Page = Convert. ToInt32 (arrPage [1]);
}
Else
{
Page = 1;
}
If (page> PageCount) {page = PageCount ;}
If (page <1) {page = 1 ;}
Return (int) page;
}
}
/// <Summary>
/// Set the page Jump URL
/// </Summary>
Private string Url
{
Get
{
String query = Request. Url. Query;
Regex regex = new Regex (@ "(page = \ d *)", RegexOptions. IgnoreCase | RegexOptions. Compiled );
If (! Regex. Match (query). Success) {query + = (query. IndexOf ("? ")>-1? "& Amp; page = 1 ":"? Page = 1 ");}
Return query;
}
}
# Endregion
# Region Public attributes
/// <Summary>
/// Page size
/// </Summary>
Public int PageSize
{
Get {return (pageSize = 0 )? 10: pageSize ;}
Set {pageSize = value ;}
}
/// <Summary>
/// Total number of records
/// </Summary>
Public int RecordCount
{
Get {return recordCount ;}
Set {recordCount = value ;}
}
/// <Summary>
/// The Name Of The paging Style
/// </Summary>
Public string CssClas
{
Get {return (string. IsNullOrEmpty (cssClass ))? "Sabrosus": cssClass ;}
Set {cssClass = value ;}
}
/// <Summary>
// Align center: center; left: left alignment; right: right alignment
/// </Summary>
Public string Align
{
Get
{
If (string. IsNullOrEmpty (align ))
Return "center ";
Switch (align. ToLower ())
{
Case "left ":
Return "left ";
Case "right ":
Return "right ";
Default:
Return "center ";
}
}
Set
{
Align = value;
}
}
Public int Width
{
Get {return width ;}
Set {width = value ;}
}
/// <Summary>
/// Whether the return page information is displayed
/// </Summary>
Public bool? EnableJumpPage
{
Get {return (enableJumpPage = null )? False: enableJumpPage ;}
Set {enableJumpPage = value ;}
}
/// <Summary>
/// Whether the page information is displayed, for example, 10/100 page, one hundred page, current 10th page
/// </Summary>
Public bool? EnablePageInfo
{
Get {return (enablePageInfo = null )? False: enablePageInfo ;}
Set {enablePageInfo = value ;}
}
# Endregion
}
}
Let's talk about some important points in this Code:
? . In c? Is not commonly used.
If it is? This is often used. What is the data type? : It indicates an empty type, which is a special value type. Its value can be null. It is used to assign a null value to the variable instead of 0 when setting the initial value. For example, int? A Indicates that null is assigned to the int type variable.
If there are two? It is used to determine and assign values. First, it determines whether the current variable is null. If yes, it can be assigned the following value; otherwise, it is skipped. For example, obj = obj1 ?? New Object ()
It indicates that if obj is not null and obj1 is assigned, the value of new Object () is equivalent to the following code:
If (obj = null)
Obj = new Object ();
Else
Obj = obj1;
Iv. css style beautification
The pipeline code is as follows. If you are interested, you can write your own Css code for beautification.
. Pagelist {height: 30px; line-height: 30px; text-align: right; margin-right: 20px ;}
. Pagelist a {padding: 3px 8px; border: 1px # CCC solid; margin: 0px 3px ;}
. PagerPrev {padding: 3px 8px; border: 1px # CCC solid; margin: 0px 3px ;}
. Current {padding: 3px 8px; border: 1px # CCC solid; margin: 0px 3px; background: # F06; color: # FFF ;}
. Disabled {padding: 3px 8px; border: 1px # CCC solid; margin: 0px 3px; background: # F6F6F6; color: #999 ;}
. PagebarTextBox {border: 1px # ccc solid; padding: 3px 8px ;}
. PagerButton {border: 1px # ccc solid; padding: 3px 8px ;}
5. Call controls
OK, our control has been developed. Here is how to use this paging control. First, we reference this control and Ccss style on the page. The Code is as follows:
Introduce the control in the page header:
<% @ Register Src = "../control/PageShow. ascx" TagName = "PageShow" TagPrefix = "uc1" %>
Introduce tables between headers:
<Link href = "../skin/purple/Pagelist.css" rel = "stylesheet" type = "text/css"/>
Compile the control code where the page is to be displayed:
<Div class = "Pagelist">
<Uc1: PageShow ID = "PageShow1" runat = "server" CssClas = "Pagelist" EnableJumpPage = "true" EnablePageInfo = "true" Width = "960"/>
</Div>
Write code in the background to implement the paging control function:
If (myp. recordCount> 20)
{
PageShow1.PageSize = 20;
PageShow1.RecordCount = myp. recordCount;
}
Else
{
PageShow1.Visible = false;
}
In this way, the paging control is developed and the final result is shown in-2.
Figure 2