Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize paging browsing of Web data by ASP.NET

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces ASP.NET how to achieve Web data paging browsing, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

When using ASP.NET to develop the query function module of Web database, in order to reduce network traffic and improve the access speed of pages, generally, all the query results will not be displayed completely in one page, but will be divided into multiple pages according to certain rules, and a certain number of records will be displayed on each page. The application of data record paging technology can improve the query performance of data and the display speed of pages, and reduce the burden of database server.

1 Overview of ASP.NET query paging technology

There are two main ways to page and display the query results of Web database, which are browser-side paging and server-side paging. Browser-side paging is to first save all the records that meet the query conditions in the database at one time, and then realize the paging display by setting the number of records to be displayed per page, and then determining the starting point and end point of the records to be displayed. Server-side paging is to read and display the specified number of records from the records that meet the query conditions each time according to the user's request.

To this end, we use server-side paging. When paging on the browser side, in order to reduce the network traffic, we use SQL Server stored procedures for paging.

2 main technologies

ADO.NET class

In the Web application system, ASP.NET uses ADO.NET to connect the Web browser in the front end of the system and the database in the background. ADO.NET is a series of classes provided by the .NET Framework that provide access to relational data, XML, and application data.

3 implementation of ASP.NET query paging technology

Here, we use System.data.SqlClient (which contains classes that access the SQL Server database) to operate on MS SQL Server2000 and use the C # language in ASP.NET to describe it. Take the "View Student message Information Module" in the "online examination system" developed by the author as an example to illustrate the development process of this technology.

3.1 connect to the database

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

ConnectionString

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

Open

Open a database connection using the property settings specified by ConnectionString

Close

Close the connection to the database

The connection string is as follows:

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

3.2 Establishment of stored procedures

CREATE PROCEDURE PageCut @ PageSize int=10 -- Page size @ PageIndex int=1-- current number of pages AS declare @ strSQL varchar @ PageIndex=1 set @ strSQL='select top'+str (@ PageSize) +'* from tdbLevMess order by rqsj' else set @ strSQL='select top'+str (@ PageSize) +'* from tdbLevMess where rqsj not in (select top'+ str ((@ PageIndex-1) * @ PageSize) + 'rqsj from tdbpres) order by rqsj' exec (@ strSQL) GO

3.3 operate the database

Query data operations can be implemented in a variety of ways, including SqlCommand objects (representing an SQL query or a stored procedure) and SqlDataAdpater objects (submitting a SqlCommand to a SQL Server database). Here, we use the SqlCommand object to manipulate the database.

The SqlCommand class is located in the System.Data.SqlClient space and represents an Transact-SQL statement or stored procedure to be executed against the SQL Server database. The properties and methods used in this example are as follows:

CommandText

Gets or sets the Transact-SQL statement or stored procedure to execute against the data source.

CommandType

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

Connection

Gets or sets the SqlConnection used by this instance of SqlCommand.

Parameters

Get the SqlParameterCollection.

ExecuteReader

Send CommandText to Connection and generate a SqlDataReader.

The SqlParameter class is located in System.Data.SqlClient space and represents a parameter of SqlCommand, or it can be a mapping to a DataSet column. The Value property used in this example (gets or sets the value of this parameter).

The implementation procedure is as follows:

SqlCommand SCM=new SqlCommand (); / define SqlCommand class SqlDataReader SDR; / / define SqlDataReader class private void GridView_Init (int PageSize,int PageIndex) {SCN.Open (); / / Open database connection SCM.Connection = SCN; SCM.CommandText = "pagecut"; / / execute stored procedure SCM.CommandType = CommandType.StoredProcedure SqlParameter [] pares = {new SqlParameter ("@ PageSize", SqlDbType.Int), new SqlParameter ("@ PageIndex", SqlDbType.Int)}; pares [0] .value = PageSize; pares [1] .value = PageIndex; SCM.Parameters.Clear () / / clear the parameter foreach (SqlParameter parameter in pares) {SCM.Parameters.Add (parameter);} SDR = SCM.ExecuteReader (); / / execute the stored procedure GridView1.DataSource = SDR; / / define GridView's data source GridView1.DataBind (); / / GridView data source binding SCN.Close () / / close connection}

3.4 implementation of the program

On the Web form, add a GridView control (for the display of data), four LinkButton (used as hyperlinks, namely the first page, the previous page, the next page, and the last page), and a Label control. Set the AllowPaging property of the GridView control (whether paging is turned on in GridView) to False. We use the Session object to save the parameters in the paging program. The specific procedures are as follows:

Protected void Page_Load (object sender, EventArgs e) {/ / Page initialization if (! IsPostBack) / / if the page is not * load {int PageCount,PageIndex,PageSize; / / define the total number of pages, current page number, number of records per page SCN.Open () SCM.CommandText = "select count (*) from tdbLevMess"; SCM.Connection = SCN; int ReCount = Convert.ToInt32 (SCM.ExecuteScalar (). ToString ()); / / Total number of dataset records SCN.Close (); Session.Add ("PageSize", 20) / / assign PageSize = Convert.ToInt16 (Session ["PageSize"]) to the shared variable PageSize records per page; / / 20 PageCount=ReCount% records per page = 0? ReCount / PageSize: ReCount / PageSize + 1; calculate the total number of pages Session.Add ("PageCount", PageCount); / / assign PageIndex = 1 to the total number of pages of shared variables PageCount; Session.Add ("PageIndex", PageIndex); / / assign GridView_Init (PageSize, PageIndex) to the total number of pages of shared variables / / the calling procedure populates the data for GridView LinkButton1.Enabled = false, LinkButton2.Enabled = false; LinkButton3.Enabled = true,LinkButton4.Enabled = true; Label1.Text = "page" + PageIndex.ToString () + "page / total" + PageCount.ToString () + "page" }} protected void LinkButton2_Click (object sender, EventArgs e) {/ / previous page button int PageIndex = Convert.ToInt16 (Session ["PageIndex"])-1; int PageCount = Convert.ToInt16 (Session ["PageCount"]); Session.Add ("PageIndex", PageIndex) Int PageSize = Convert.ToInt16 (Session ["PageSize"]); if (PageIndex < = 1) {LinkButton1.Enabled = false; LinkButton2.Enabled = false;} LinkButton3.Enabled = true; LinkButton4.Enabled = true GridView_Init (PageSize, PageIndex); Label1.Text = "th" + PageIndex.ToString () + "Page / Total" + PageCount.ToString () + "Page";}

We only give the code for LinkButton2 (to scroll up the page), and other hyperlink codes are similar.

Thank you for reading this article carefully. I hope the article "how to realize Web data pagination browsing by ASP.NET" shared by the editor will be helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report