One unavoidable problem in web design is pagination. When the data volume is very large, we cannot display all the data on a single page. If this happens, it will seriously affect its aesthetics. At this time, the page display becomes our hero. Of course, there are two types of paging, one is true paging, and the other is false paging, that is:
False paging: select all records from the database and go to the paging page.
Real page: select records on the current page from the database
First, let's talk about the built-in paging function (false paging ):
In. net, the GridView control comes with the paging display function. Of course, the operation is very simple. It is divided into two steps:
1. Add a GridView control on the interface and set its attribute AllowPaging to True and PageSize to n (n is the number of records displayed on each page ), the setting is successful.
2. Bind the data to the GridView control. The details are as follows:
Protected void Page_Load (object sender, EventArgs e) {if (! Page. isPostBack) {// call the data binding method when the page is loaded for the first time </span> BindNews ();}} /// <summary> /// bind data method /// </summary> </span> private void BindNews () {// query data, and bind the data to GridView1 </span> GridView1.DataSource = newNewsManager (). selectAll (); GridView1.DataBind ();} /// <summary> /// data binding function during page feed // </summary> /// <paramname = "sender"> </param> // <paramname = "e"> </param> </span> protected voidGridView1_PageIndexChanging (object sender, gridViewPageEventArgs e) {// obtain the index of the current page </span> GridView1.PageIndex = e. newPageIndex; // re-bind data </span> BindNews () ;}</span>
You can easily obtain data by page through the above steps. Although the desired results can be achieved, there are still some problems. For example, the data binding method above is bound with all the news, and all the records in the database will be queried each time it is executed, when the number of records reaches a certain level, such as 0.5 million, 1 million, and so on, how long the binding time will be. Therefore, the above situation is defined as false paging. In comparison, the real paging solves this problem. Let's witness the magic of the real paging:
Data Query is required before paging. Therefore, a stored procedure is created here to query paging data, as shown in the following code:
<Span style = "font-size: 18px;"> <span style = "white-space: pre"> </span> alter procedure [dbo]. [aspPageChoose] </span> @ startPageint, </span> @ endPageint </span> AS </span> BEGIN ---- create a temporary table, used to store </span> withtemptbl as (</span> selectROW_NUMBER () OVER (order by id desc) as rowNum, * from news </span>) </span> select * from temptbl where rowNum between @ startPage and @ endPage </span> END </span>
The second is the establishment of the U layer. On the interface, place two controls, AspNetPage and GridView, respectively. Set the number of the two controls displayed on each page to the corresponding value, finally, bind the data and display it as follows:
Protected voidPage_Load (object sender, EventArgs e) {if (! Page. isPostBack) {// call the data binding method when the page is loaded for the first time </span> anp. recordCount = <span style = "color: # ff0000;"> total number of news </span>; int startpage = 1; int endpage = 3; DataTable dt = newNewsManager (). selectAspNetPage (startpage, endpage); GridView1.PageSize = endpage-startpage + 1; GridView1.DataSource = dt; GridView1.DataBind ();}} // display data on a page </span> protected voidanp_PageChanged (object sender, EventArgs e) {int startpage = anp. startRecordIndex; int endpage = anp. endRecordIndex; DataTable dt = newNewsManager (). selectAspNetPage (startpage, endpage); GridView1.PageSize = endpage-startpage + 1; GridView1.DataSource = dt; GridView1.DataBind () ;}</span>
Summary: It is often said that the comparison of true knowledge can be clearly displayed through the comparison above. The real page opens the door for us, and brings us great benefits for programming. The above is purely self-developed. If you have any shortcomings, please give us some advice.