Asp. NET processing data paging (1)

Source: Internet
Author: User
Tags count implement integer prev reset servervariables tostring trim
asp.net| Pagination | When the data is programmed in the ASP's database, because of the size of the browser, in order to see more data records, to achieve a more intuitive effect, we have these data records into several pages, through the data navigation buttons (or other hyperlinks), paging browsing. In fact, this kind of data records paging browsing can also be achieved in asp.net. And in the process of implementation than in the process of ASP to appear more organized and more clear, and easier.

There are two basic types of paging browsing data logging through browsers. Other types of paging browsing are either changes to these two types or a combination of these two types. The following two images are shown in the specific manner:

Here's a look at the specific implementation process for the two paging browsing data records in asp.net:

First of all, to introduce the database we use, in this article for convenience, we use the local database Access 2000, the database name is ' Data.mdb ', which contains a datasheet ' Tblitem '. The data table is structured as follows:

Field Name field type
ItemID Automatic Numbering
ItemName Text Type


If you're using a different database, just make a simple change to the program described below. This is described below.

A The software environment for the design and operation of this article:

(1) Microsoft company Windows 2000 Server Edition

(2). Net FrameWork SDK Beta 2

Two The first of the key steps for paging through data logging and how to implement them:

(1). First get the hyperlink string for the initial browsing data record:

This is really critical, because in the first page of paging in the ' home ', ' next page ', and so on, by adding to the hyperlink string to browse the page after the parameters to achieve, in this article in the program is through the Getpagename () function to achieve. This function is specific as follows:

Function Getpagename () as String
Dim Str as String
Dim Pos as Short
STR = Request.ServerVariables (' Script_name '). Trim ()
Pos = Str.lastindexof ('/')
If Pos >= 0 Then
Return str.substring (Pos + 1)
Else
Return STR
End If
End Function


(2). To get the total number of data records you want to browse:

In this article, for convenience, we are browsing all the records in the datasheet ' Tblitem '. Asp. NET page through ado.net to get data table ' Tblitem '. The following code is the program code that uses Ado.net to get the total number of records in the ' Tblitm ' table:

<% @ Page Language = ' VB '%>
<% @ Import Namespace = ' System.Data '%>
<% @ Import Namespace = ' System.Data.OleDb '%>
<script runat = ' server ' >
Dim strconn As String ' defines a data connection string
Dim SQL as String ' defines an SQL statement
Dim Odconn as OleDbConnection
Dim Odadapt as OleDbDataAdapter
Dim DS as DataSet ' Create DataSet object
Dim DT as DataTable ' Create DataTable Object
Dim Nstart as Integer ' holds the starting record number of the current page
Dim Nend as Integer ' holds the current page's terminating record serial number
Dim I as Integer

' Confirm the number of pages you want to browse
Npage = Convert.ToInt32 (Request.QueryString (' Page '))
SQL = ' SELECT * from Tblitem '

' Create a data connection string
strconn = ' Provider = microsoft.jet.oledb.4.0; ' & _
' Data Source = ' & Server.MapPath (' Data.mdb ') & '; ' & _
' User ID =; Password =; '
Try
' Get the total number of data records
Odconn = New OleDbConnection (strconn)
Odadapt = New OleDbDataAdapter (SQL, Odconn)
DS = New DataSet
Odadapt.fill (DS)
DT = DS. Tables (0)
' Get the total number of data records
Nreccount = DT. Rows.Count
Catch e as Exception
Response.Write (' Error message: <b> ' & e.message & ' </b><p> ')
Nreccount = 0
End Try
</script >


(3). Calculate the total number of pages in the browsing data record:

In the browsing page, we found that each page only browsed 5 records, you can modify the program to define a constant ' record_per_page ' to change the number of browsing data records per page. After you know the total number of data records to browse, use the following code to calculate the total number of pages required to display the data records:

Const Record_per_page as Short = 5 ' defines the number of records displayed per page
Dim Npagecount as Integer saves total number of data pages
Dim npage as Integer ' store to browse the current data page number
Npagecount = nreccount \ Record_per_page
If nreccount Mod record_per_page > 0 Then
Npagecount + 1
End If
' Confirm that the page parameters in the Navigation command are out of bounds and reset the page number if they are crossed
If Npage < 1 Then
Npage = 1
End If
If npage > Npagecount Then
Npage = Npagecount
End If


(4). How to implement data navigation:

In fact, data navigation through the parameter ' page ' assignment to achieve, where the ' Npage ' in the program is the current data page number, ' Npagecount ' is the sum of the data page. The following are the specific implementation codes for implementing these data navigation:

Response.Write (' <p > Data navigation: <a HREF = ' & Script_name & _
'? Page= ' & (1). ToString () & _
"' > Home </a > ')
Response.Write (")"
' Browse ' prev ' approach
Response.Write (' <a HREF = ' ' & Script_name & _
'? Page= ' & (nPage-1). ToString () & _
' > previous page </a > ']
Response.Write (")"
' Browse ' next page ' approach
Response.Write (' <a HREF = ' ' & Script_name & _
'? Page= ' & (Npage + 1). ToString () & _
"' > Next page </a > ')
Response.Write (")"
' Browse ' last ' approach
Response.Write (' <a HREF = ' ' & Script_name & _
'? Page= ' & (Npagecount). ToString () & _
"' > Last </a > ')
' Show current page and total pages
Response.Write (' & ' page: ' & Npage.tostring () & '/' & Npagecount.tostring () & ' <br > ' )


(5). Display data records for different pages:

Get the ' page ' value based on the hyperlink string, and then, based on this value, get the starting record number and the ending record number to display on this page, and then use a loop to show the records. The following line of code reads the parameter ' Page ':

Npage = Convert.ToInt32 (Request.QueryString (' Page '))


The following code is based on the page you want to get the starting record number and the end record number, and through the screen display:

Dim Nstart as Integer ' holds the starting record number of the current page
Dim Nend as Integer ' holds the current page's terminating record serial number
Dim I as Integer
Nstart = Record_per_page * (nPage-1)
Nend = Nstart + record_per_page-1
If nend > NRecCount-1 Then
Nend = nRecCount-1
End If
' Output the record in the screen
For i = Nstart to Nend
Response.Write (DT. Rows (i) (' ItemName ') & ' <br > ')
Next

In the two paging browsing record types described in this article, we use simplified processing for data display. We know that in the browser, browsing records are generally implemented through the form of DBGrid, which is also a good implementation, the reader only a little change in this article in the program code can be achieved.

Three The first page browses the complete program code of the data Record (no1.aspx):

To organize the implementation of these steps above, you can get the following complete code:

<% @ Page Language = ' VB '%>
<% @ Import Namespace = ' System.Data '%>
<% @ Import Namespace = ' System.Data.OleDb '%>
<script runat = ' server ' >
Const Record_per_page as Short = 5 ' defines the number of records displayed per page
Private Script_name as String

Sub Page_Load (Source as Object, e as EventArgs)
Script_name = Getpagename ()
' The first way to page-display data
Showrecords ()
End Sub
' Get start Browse hyperlink string
Function Getpagename () as String
Dim Str as String
Dim Pos as Short
STR = Request.ServerVariables (' Script_name '). Trim ()
Pos = Str.lastindexof ('/')
If Pos >= 0 Then
Return str.substring (Pos + 1)
Else
Return STR
End If
End Function

' The function of this function is paging to display records in the database
Private Sub showrecords ()
Dim strconn As String ' defines a data connection string
Dim SQL as String ' defines an SQL statement
Dim Odconn as OleDbConnection
Dim Odadapt as OleDbDataAdapter
Dim DS as DataSet ' Create DataSet object
Dim DT as DataTable ' Create DataTable Object
Dim Nreccount as Integer save Total Records
Dim Npagecount as Integer saves total number of data pages
Dim npage as Integer ' store to browse the current data page number
Dim Nstart as Integer ' holds the starting record number of the current page
Dim Nend as Integer ' holds the current page's terminating record serial number
Dim I as Integer

' Confirm the number of pages you want to browse
Npage = Convert.ToInt32 (Request.QueryString (' Page '))
SQL = ' SELECT * from Tblitem '

' Create a data connection string
strconn = ' Provider = microsoft.jet.oledb.4.0; ' & _
' Data Source = ' & Server.MapPath (' Data.mdb ') & '; ' & _
' User ID =; Password =; '
Try
' Get the total number of data records
Odconn = New OleDbConnection (strconn)
Odadapt = New OleDbDataAdapter (SQL, Odconn)
DS = New DataSet
Odadapt.fill (DS)
DT = DS. Tables (0)
Nreccount = DT. Rows.Count
Catch e as Exception
Response.Write (' Error message: <b> ' & e.message & ' </b><p> ')
Nreccount = 0
End Try

' Determine if there is a data record
If nreccount > 0 Then
' Determine the number of pages to display for the data record
Npagecount = nreccount \ Record_per_page
If nreccount Mod record_per_page > 0 Then
Npagecount + 1
End If

' Confirm that the page parameters in the Navigation command are out of bounds and reset the page number if they are crossed
If Npage < 1 Then
Npage = 1
End If
If npage > Npagecount Then
Npage = Npagecount
End If

Response.Write (' A total of data records ' & Nreccount.tostring () & ' ' & '. <br > ')
Response.Write (' <p > <b > First page display as: </b > <p > ')

' Confirm the start and end records of the current page
Nstart = Record_per_page * (nPage-1)
Nend = Nstart + record_per_page-1
If nend > NRecCount-1 Then
Nend = nRecCount-1
End If
' Output the record in the screen
For i = Nstart to Nend
Response.Write (DT. Rows (i) (' ItemName ') & ' <br > ')
Next
End If
' Browse ' home ' approach
Response.Write (' <p > Data navigation: <a HREF = ' & Script_name & _
'? Page= ' & (1). ToString () & _
"' > Home </a > ')
Response.Write (")"
' Browse ' prev ' approach
Response.Write (' <a HREF = ' ' & Script_name & _
'? Page= ' & (nPage-1). ToString () & _
' > previous page </a > ']
Response.Write (")"
' Browse ' next page ' approach
Response.Write (' <a HREF = ' ' & Script_name & _
'? Page= ' & (Npage + 1). ToString () & _
"' > Next page </a > ')
Response.Write (")"
' Browse ' last ' approach
Response.Write (' <a HREF = ' ' & Script_name & _
'? Page= ' & (Npagecount). ToString () & _
"' > Last </a > ')
' Show current page and total pages
Response.Write (' & ' page: ' & Npage.tostring () & '/' & Npagecount.tostring () & ' <br > ' )
End Sub
</script >

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.