Previous ArticleArticleThe stored procedure is used for paging, but there are defects, noCodeReuse: You need to write the Stored Procedure for different tables or views. This is troublesome and the code reusability is poor. This article uses a user control to implement the page layer of Data Paging for code reuse.
Pagination is implemented using user-defined controls. The following two methods are provided:
(1). the aspnetpager component of wuqiwa (recommended)
First, download the DLL file. : Aspnetpager Page Control
In the toolbox, right-click and select items to add the downloaded DLL file.
Then,Aspnetpager Page Control, Drag to the page, and drag a data display control, gridview or repeater.
Finally, write the background Code as follows:
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Binddata ();
}
}
// obtain and bind data
Public void binddata ()
{< br> // connect to the database
string strcon = "Server = .; database = northwind; uid = sa; Pwd = ";
sqlconnection con = new sqlconnection (strcon);
string SQL =" select * from MERs ";
sqldataadapter SDA = new sqldataadapter (SQL, con);
dataset DS = new dataset ();
SDA. fill (DS);
// how to use the aspnetpager paging Control
dataview DV = Ds. tables [0]. defaultview;
// use the pageddatasource class
pageddatasource PPS = new pageddatasource ();
aspnetpager1.recordcount = DV. count; // The total number of records obtained
PPS. datasource = DV;
PPS. allowpaging = true;
// obtain the index of the current page
PPS. currentpageindex = aspnetpager1.currentpageindex-1;
PPS. pagesize = aspnetpager1.pagesize; // obtain the number of records displayed on each page
gridview1.datasource = PDS; // specify the data source
gridview1.databind ();
}
Protected void aspnetpager1_pagechanged (Object sender, eventargs E)
{
Binddata ();
}
}
This is just a few simple steps. How can we define and apply styles for the aspnetpager paging control. Very simple, as shown below
Add the following style to <style type = "text/CSS"> </style> in the source, and then add the attribute after the aspnetpager page control.
(1). NetEase style:
CSS style:
. Anpager. ECC {Background: #1f3a87 none repeat scroll 0 0; Border: 1px solid # cccccc; color: # ffffff; font-weight: bold; margin: 5px 4px 0 0 0; padding: 4px 5px 0 ;}
. Anpager A {Background: # ffffff none repeat scroll 0 0; Border: 1px solid # cccccc; color: #1f3a87; margin: 5px 4px 0 0; padding: 4px 5px 0; text-Decoration: None}
. Anpager A: hover {Background: #1f3a87 none repeat scroll 0 0; Border: 1px solid #1f3a87; color: # ffffff ;}
Property settings: cssclass = "anpager" currentpagebuttonclass = "cardiopulmonary bypass"
(2 ). pat network style:
CSS style:
. paginator {Font: 11px Arial, Helvetica, sans-serif; padding: 10px 20px 10px 0; margin: 0px ;}< br>. paginator A {padding: 1px 6px; Border: solid 1px # DDD; Background: # FFF; text-Decoration: none; margin-Right: 2px}
. paginator A: visited {padding: 1px 6px; Border: solid 1px # DDD; Background: # FFF; text-Decoration: none;}
. paginator. |. paginator A: hover {color: # FFF; Background: # ffa501; border-color: # ffa501; text-Decoration: none;}
property settings: cssclass = "paginator" currentpagebuttonclass = "cardiopulmonary bypass"
(3). Thunder style:
CSS style:
. Pages {color: #999 ;}
. Pages ,. pages. CPB {text-Decoration: none; float: Left; padding: 0 5px; Border: 1px solid # DDD; Background: # FFFF; margin: 0 2px; font-size: 11px; color: #000 ;}
. Pages a: hover {background-color: # e61636; color: # FFF; Border: 1px solid # e61636; text-Decoration: none ;}
. Pages. ECC {font-weight: bold; color: # FFF; Background: # e61636; Border: 1px solid # e61636 ;}
Property settings: cssclass = "pages" currentpagebuttonclass = "cardiopulmonary bypass"
(2). zhangziyang's ASP. NET scalable paging User Control
This method is complicated to implement, without the above simplicity. It uses the following control idea:
It is based on an HTTP request and retrieves URL parameters for paging. Instead of collecting http post requests, it obtains hidden form data items for paging..
Interested friends can refer to: http://www.tracefact.net/asp-net/Extensible-Paging-User-Control.aspx
To sum up the following, the first control uses PostBack paging, and the second control uses URL paging.
In addition to the default PostBack paging mode similar to the DataGrid and gridview, aspnetpager also supports pagination through URLs, like most ASPProgramThe URL paging method allows you to enter the corresponding address in the browser address bar to directly enter the specified page. You can also enable the search engine to search for the content of all pages, therefore, it is user-friendly and search engine friendly. For the differences between URL paging and PostBack paging, see the following table:
Comparison between PostBack pagination and URL pagination
| |
PostBack page |
URL pagination |
| Paging Mode |
Pass paging information through page return. |
The paging information is transmitted by redirecting to the specified URL. |
| Paging Performance |
You can save all paging information in viewstate. For example, you can obtain the total number of records to be paged when loading the page for the first time. You do not need to access the database again to obtain this value, however, it can be obtained from viewstate after sending back, which reduces the number of database accesses and increases the paging speed. |
The page is reinitialized every time it is paginated. Data on all pages except the current page index passed through the URL must be retrieved again, for example, you have to retrieve the total number of records to be paged from the database each time, so the speed is slightly lower than that of the PostBack paging method, however, you can save the total number of records in the cache or session to achieve the same paging performance as PostBack paging. |
| Flexibility |
Visitors can dynamically change the paging attribute and keep the changed attribute value after sending it back. |
Attributes can be changed dynamically, but the changed attribute values are not retained after the page Jump, and are restored to the initial value. |
| Direct Access |
Visitors cannot access other pages except the first page by entering a URL. |
Visitors can access any page by entering the URL of the corresponding page. |
| Search engine friendly |
The search engine can only retrieve the first page. |
Search engines can search all pages. |
Thank you for reading this article. I hope this article will help you!