ASP (four)-using the GridView to realize the true and false pagination

Source: Internet
Author: User

This is the time to read the ASP. But feel that their study seems to have not consolidated well, and then in the library borrowed a few books about the ASP. I feel really good about the main small demo that I can achieve for ASP. But I know that only a real use can be harvested, and their own awareness of the need to further study. In this part of the study oneself also is to have a main understanding of the pagination, also uses it to make out of several main demo. Then let's take a look at some of the uses of this control for true and false pagination.

I. What is true and false paging

1. False paging:

False paging, although it was paged on the interface, did not implement paging. Every time you click on the number of pages from the database to tell the whole data again, that is, every time you query from the database once so it will affect his query speed. So false paging, though it is the implementation of pagination on the interface, but still did not achieve real paging.

2. True paging:

So we know that if we want to really implement paging, we just have to query the data for the page. This can reduce the amount of data per query and also improve the efficiency of the query, the real implementation of the page to look up a page.

Two. Basic implementation

1. Fake page break

The video in our video has a long history so when we learned it, the control changed its name, but it was still in the same place. It's going to take so many steps. 1> to change his AllowPaging property to true in the GridView, and then set pagesize to the number of pages you want to page. Then the next step is to bind the control to the database

protected void Page_Load (object sender, EventArgs e) {//page first load if (!                  Page.IsPostBack) {//bind data Gridview1.datasource = MyData ();              Gridview1.databind ();              }} protected void Gridview1_pageindexchanging (object sender, Gridviewpageeventargs e) {              Gridview1.pageindex = E.newpageindex;                        DataTable dt = MyData ();              Gridview1.datasource = DT;            Gridview1.databind (); } private static DataTable MyData () {SqlConnection con = new SqlConnection ("server= .;              Database=northwind;uid=sa;password=gss123 "); Con.              Open ();              String strcmd = "SELECT * from comment";              SqlCommand cmd = new SqlCommand (Strcmd,con); SqlDataReader SDR = cmd.              ExecuteReader ();              DataTable dt=new DataTable (); Dt.   Load (SDR);           Con.              Close ();              return DT;   }      }

The above is the data binding for false paging.

2. True pagination

True paging but it takes a little effort, true paging is required to aspnetpaper this third-party control, the need to download his DLL file and then drag to the toolbox and then use it, he is a tool to turn the page

 protected void Page_Load (object sender, EventArgs e) {if (! Page.IsPostBack) {//Window clock-in, starting data number is 0, terminating data number is per page number of bars int intstartindex = ANP .                  PageSize * 0; int intendindex = ANP.                PageSize * 1; <span style= "color: #ff0000;" ><strong> ANP. RecordCount = Myalldata (). Rows.count;</strong></span>//bind data Gridview1.datasource = Mypartdata (Intstar                  Tindex, Intendindex);              Gridview1.databind (); }}///<summary>//Change the number of pages displayed///</summary>//<param Nam E= "Sender" ></param>//<param name= "E" ></param> protected void anp_pagechanged (obj              ECT sender, EventArgs e) {//start data Number = information capacity per page * (current page-1) +1. Example: page sixth, each page shows 5, page sixth of the starting data =5*5+1=26; int INTSTARTINDEX=ANP. PageSize * (ANP. Currentpageindex-1) +1; int intendindex = ANP. PageSize * ANP.              CurrentPageIndex;              Gridview1.datasource = Mypartdata (Intstartindex,intendindex);            Gridview1.databind (); }///<summary>/////</summary>//<param name= "Intstartindex" > Start Data Number </param>//<param name= "Intendindex" > End data Number </param>//<returns> table              Lattice </returns> private static DataTable mypartdata (int intstartindex,int intendindex) { SqlConnection con = new SqlConnection ("server=.;              database=newssystem;uid=sa;password=123456 "); Con.              Open ();              String strcmd = "SELECT * from comment";              SqlCommand cmd = new SqlCommand (Strcmd,con); SqlDataReader SDR = cmd.              ExecuteReader ();              DataTable dt=new DataTable (); Dt.              Load (SDR); Con.              Close ();            return DT; }//&LT;summary>///all enquiries///</summary>//<returns></returns> privat e static DataTable Myalldata () {SqlConnection con = new SqlConnection ("server=.;              Database=northwind;uid=sa;password=gss123 "); Con.              Open ();              String CMD = "SELECT * from comment";              SqlCommand cmd = new SqlCommand (cmd, con); SqlDataReader SDR = cmd.              ExecuteReader ();              DataTable dt = new DataTable (); Dt.              Load (SDR); Con.              Close ();            return DT;   }      }

After this period of study I learned about the true and false page of the matter so, in the future to continue to learn some of his things, as well as I am clear that sometimes I can not go through the time to see how others are doing, but do not use the full reference, but also to have their own experience.


ASP (four)-using the GridView to realize the true and false pagination

Related Article

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.