True/false Paging

Source: Internet
Author: User

When I was doing a news publishing system, I thought there was no paging implementation in the video, so I spent a day writing the video. Of course, errors were constantly occurring. Fortunately, I finally made a solution while learning it. Later I saw the implementation method in the video, which is a little different from myself. Here I will share my experience.

When we want to display a large amount of data, we usually use the paging display method. Pagination includes real and false pages.

False Paging: Retrieve all data from the database and display it on pages. It takes a long time to access the database once, but the selected data volume is large. However, each page is displayed directly and quickly, avoiding multiple accesses to the database.

Real Paging: Determine the quantity and content to be displayed, and then retrieve the small amount of data from the database each time. The advantage is that the data volume is small, and the disadvantage is that the database is frequently accessed. In large websites, real pages are often used, such as Baidu's image retrieval.

The news publishing system uses real paging. The Web layer is displayed using the AspNetPager control and Repeater control. The implementation idea is as follows:

Query the total number of records of all news to obtain the RecordCount attribute value of AspNetPager. Set the PageSize attribute to determine the number of records displayed on each page. The preceding two attributes determine how many pages are displayed for all these records. The StartRecordIndex and EndRecordIndex attributes of the control can determine the index number of the record to be displayed on the current page, so that you can easily query from the database. The queried able is the data source bound to the repeater.

The control attributes used here are different from those in the video, so the SQL statements are also different.

See the code implementation:

WebFront end:

<Webdiyer: aspNetPager ID = "anp" runat = "server" FirstPageText = "home" LastPageText = "last" NextPageText = "Next" PrevPageText = "previous" OnPageChanged = "anp_PageChanged" PageSize =" 5 "AlwaysShow =" true "> </webdiyer: aspNetPager>

Background code:

 if (!Page.IsPostBack)                {                    anp.RecordCount = nm.Count();                    BindNews();                }
Protected void anp_PageChanged (object sender, EventArgs e) {BindNews () ;}# region binds the news list private void BindNews () {int startIndex = anp. startRecordIndex; int endIndex = anp. endRecordIndex; DataTable dt = nm. selectToPage (startIndex, endIndex); repNews. dataSource = dt; repNews. dataBind () ;}# endregion

Layer D:
/// <Summary> select news by PAGE ///// </summary> /// <param name = "startIndex"> the first news index on the selected page </param> /// <param name = "endIndex"> last news index of the selected page </param> /// <returns> news content of the selected page </returns> public DataTable SelectToPage (int startIndex, int endIndex) {DataTable dt = new DataTable (); SqlParameter [] paras = new SqlParameter [] {new SqlParameter ("@ startIndex", startIndex ), new SqlParameter ("@ endIndex", endIndex)}; string SQL = "select * from (select ROW_NUMBER () over (order by id desc) as row, T. * from news T) as TT where TT. row between @ startIndex and @ endIndex "; dt = sqlhelper. executeQuery (SQL, paras, CommandType. text); return dt ;}/// <summary> total news ///// </summary> /// <returns> </returns> public int Count () {string SQL = "select count (*) from news"; DataTable dt = new DataTable (); dt = sqlhelper. executeQuery (SQL, CommandType. text); int count = Convert. toInt32 (dt. rows [0] [0]. toString (); return count ;}

There are two points to note:

FirstTo query the total number of news, call sqlhelper's query method without parameters to obtain a table with only one row and one column. At first, I thought it was a value. An error occurred, in layer D, retrieve the first column in the first row of the table and return it to layer B.

SecondAn error occurred while adding the control.

If the reference is correct, the source code is registered in the header, and the error message still exists after re-compiling or even restarting VS, the runtime will not be affected. Let's do this for the time being. If you know the reason, please kindly advise.

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.