ASP. NET query paging technology: Implement pagination of Web Data

Source: Internet
Author: User
Tags web database

Use ASP. when developing the Web database query function module, to reduce network traffic and increase the page access speed, the query results are generally not displayed on a single page, instead, it is divided into multiple pages for display according to certain rules. A certain number of records are displayed on each page. The data record paging technology can improve data query performance and page display speed, reducing the burden on database servers.


1 Overview of ASP. NET query paging technology
The Web database query results are displayed by page in two ways: browser-side paging and server-side paging. The browser saves all records in the database that meet the query conditions at one time, and then sets the number of records to be displayed on each page, then determine the start and end points of the record to display the records by page. The server page displays and reads the specified number of records from the records that meet the query conditions based on the user's requests.
To this end, we use the server side paging method. When paging is performed on the browser side, we use the SQL Server Stored Procedure for paging to reduce network traffic.


2 main technologies
ADO. NET class
In the Web application system, ASP. NET uses ADO. NET to connect the front-end Web browser of the system with the back-end database. ADO. NET is a series of classes provided by. NET Framework. It provides access to relational data, XML, and application data.

3 Implementation of ASP. NET query paging technology
Here, we use System. data. SqlClient to include the class for accessing the SQL Server database) to operate ms SQL Server2000 and describe it using the C # language in ASP. NET. Taking the "view student message module" in the "online examination system" developed by the author as an example to describe the development process of this technology.

3.1 connect to the database
To access data, you must first connect to the database. Here we use the System. data. SqlClient. SqlConnection class to connect to the Student database in SQL Server. The main attributes and methods used are as follows:

ConnectionString
Gets or sets the string used to open the SQL Server database.

Open
Use the attribute settings specified by ConnectionString to open a database connection

Close
Close the connection to the database

The connection string is as follows:

 
 
  1. SqlConnection SCN = new SqlConnection("server=;database=BSNetExam;uid=sa;pwd=");   

3.2 creation of Stored Procedures

 
 
  1. Create procedure PageCut
  2. @ PageSizeInt=10, -- Page size
  3. @ PageIndexInt=1-- Current page number
  4. AS
  5. Declare @ strSQL varchar (500)
  6. If @PageIndex=1
  7. Set @StrSQL='Select top'+ Str (@ PageSize) + '* from tdbLevMess order by rqsj'
  8. Else
  9. Set @StrSQL='Select top'+ Str (@ PageSize) + '* from tdbLevMess where rqsj not in
  10. (Select top '+ str (@ PageIndex-1) * @ PageSize) + 'rqsj from tdbpres) order by rqsj'
  11. Exec (@ strSQL)
  12. GO

3.3 database operations

Data Query operations can be performed in multiple ways. Commonly used objects include SqlCommand objects that represent an SQL query or a stored procedure) and SqlDataAdpater objects that submit a SqlCommand to the SQL Server database ). Here, we use the SqlCommand object to operate the database.

The SqlCommand class is located in the space of System. Data. SqlClient, indicating a Transact-SQL statement or stored procedure to be executed on the SQL Server database. The attributes and methods used in this example are as follows:


CommandText
Obtain or set the Transact-SQL statement or stored procedure to be executed on the data source.

CommandType
Gets or sets a value indicating how the CommandText attribute is interpreted.

Connection
Obtain or set the SqlConnection used by this instance of SqlCommand.

Parameters
Obtain SqlParameterCollection.

ExecuteReader
Send CommandText to Connection and generate a SqlDataReader.

The SqlParameter class is located in the space of System. Data. SqlClient, which indicates the SqlCommand parameter or its datasing to the DataSet column. In this example, the Value attribute is used to obtain or set the Value of this parameter ).

The implementation procedure is as follows:

 
 
  1. SqlCommandSCM=NewSqlCommand (); // defines the SqlCommand class
  2. SqlDataReader SDR; // defines the SqlDataReader class
  3. Private void GridView_Init (int PageSize, int PageIndex)
  4. {
  5. SCN. Open (); // Open the database connection
  6. SCM. Connection=SCN;
  7. SCM. CommandText="Pagecut"; // Execute the Stored Procedure
  8. SCM. CommandType= CommandType. StoredProcedure;
  9. SqlParameter []Pares= {New SqlParameter ("@ PageSize", SqlDbType. Int ),
  10. New SqlParameter ("@ PageIndex", SqlDbType. Int )};
  11. Pares [0]. Value=PageSize;
  12. Pares [1]. Value=PageIndex;
  13. SCM. Parameters. Clear (); // Clear Parameters
  14. Foreach (SqlParameter parameter in pares)
  15. {
  16. SCM. Parameters. Add (parameter );
  17. }
  18. SDR=SCM. ExecuteReader (); // executes the Stored Procedure
  19. GridView1.DataSource=SDR; // Define the data source of the GridView
  20. GridView1.DataBind (); // binds the GridView data source.
  21. SCN. Close (); // Close the connection
  22. }

3.4 Program Implementation

On the Web form, add a GridView Control for data display), four linkbuttons for hyperlink, home page, Previous Page, next page, and last page), and a Label control. Set the AllowPaging attribute of the GridView control to False. We use the Session object to save parameters in the paging program. The specific procedure is as follows:

 
 
  1. Protected void Page_Load (object sender, EventArgs e)
  2. {// Page Initialization
  3. If (! IsPostBack) // If the page is not loaded for the first time
  4. {
  5. Int PageCount, PageIndex, PageSize; // defines the total number of pages, the current page number, and the number of records per page.
  6. SCN. Open ();
  7. SCM. CommandText="Select count (*) from tdbLevMess";
  8. SCM. Connection=SCN;
  9. IntReCount=Convert. ToInt32 (SCM. ExecuteScalar (). ToString (); // total number of dataset records
  10. SCN. Close ();
  11. Session. Add ("PageSize", 20); // assign a value to the number of records on each page of the shared variable PageSize
  12. PageSize=Convert. ToInt16 (Session ["PageSize"]); // 20 records per page
  13. PageCount=ReCount%PageSize= 0? ReCount/PageSize: ReCount/PageSize + 1; // calculates the total number of pages on the page.
  14. Session. Add ("PageCount", PageCount); // assign a value to the total number of pages of the shared variable PageCount
  15. PageIndex=1;
  16. Session. Add ("PageIndex", PageIndex); // assign a value to the total number of pages of the shared variable PageIndex
  17. GridView_Init (PageSize, PageIndex); // The call process fills data in the GridView
  18. LinkButton1.Enabled=False,LinkButton2.Enabled=False;
  19. LinkButton3.Enabled=True,LinkButton4.Enabled=True;
  20. Label1.Text="Th"+ PageIndex. ToString () + "Page/total" + PageCount. ToString () + "page ";
  21. }
  22. }
  23. Protected void LinkButton2_Click (object sender, EventArgs e)
  24. {// Previous page button
  25. IntPageIndex=Convert. ToInt16 (Session ["PageIndex"])-1;
  26. IntPageCount=Convert. ToInt16 (Session ["PageCount"]);
  27. Session. Add ("PageIndex", PageIndex );
  28. IntPageSize=Convert. ToInt16 (Session ["PageSize"]);
  29. If (PageIndex<= 1)
  30. {
  31. LinkButton1.Enabled=False;
  32. LinkButton2.Enabled=False;
  33. }
  34. LinkButton3.Enabled=True;
  35. LinkButton4.Enabled=True;
  36. GridView_Init (PageSize, PageIndex );
  37. Label1.Text="Th"+ PageIndex. ToString () + "Page/total" + PageCount. ToString () + "page ";
  38. }

We only provide code implementation for LinkButton2 to flip pages). Other hyperlink codes are similar to this one.

4 Conclusion

ASP. the NET query paging technology is an important task frequently encountered in Web application system development. Selecting an efficient query paging technology not only greatly improves the response speed, but also improves the overall system performance, saves valuable server resources.

  1. Introduction to ASP. NET page Lifecycle
  2. Detailed research on ASP. NET data verification technology
  3. Analysis of validation groups in ASP. NET data verification
  4. Analysis on the use of ASP. NET data verification controls
  5. Analysis on five common controls for ASP. NET data verification

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.