Asp. NET processing data paging (2)

Source: Internet
Author: User
Tags tostring
asp.net| Pagination | data four. The second page browses the key steps of data logging and how to implement it: in fact, these two kinds of paging methods in the design of the same, in the second method, the previous key steps and the first page is almost the same, but also to get the total number of browsing data records, set each page to display the number of data recorded, Figure out how many data pages are in total and so on. The method of implementing these steps can be referred to the first method. The main difference between the second paging method and the first paging method is the implementation of the data navigation method. The following code functionality is the implementation of the second paging method data navigation:

Response.Write (' <p > Data navigation: ')
Npageend = Npage + 3
If npageend > Npagecount
Npageend = Npagecount
End If
For i = 1 to Npageend
If i = Npage Then
Response.Write (' <b > ' & i.ToString () & ' </b > ')
Else
Response.Write (' <a HREF = ' ' & Script_name & _
'? Page= ' & (i). ToString () & _
' > ' & i.ToString () & ' </a > ')
End If
Next

If Npageend < Npagecount Then
Response.Write (' <a HREF = ' ' & Script_name & _
'? Page= ' & (Npageend + 1). ToString () & _
"> More ... </a >")
End If


Five The second page browses the complete source code (no2.aspx) of the data record:

No2.aspx and no1.aspx in the design of ideas and methods are roughly the same, the following is the source of the No2.aspx program, specifically as follows:

<% @ 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 second 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

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 Npageend as Integer stores the serial number of the last side of the current page
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

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 > Second 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
Response.Write (' <p > Data navigation: ')
Npageend = Npage + 3
If npageend > Npagecount
Npageend = Npagecount
End If
For i = 1 to Npageend
If i = Npage Then
Response.Write (' <b > ' & i.ToString () & ' </b > ')
Else
Response.Write (' <a HREF = ' ' & Script_name & _
'? Page= ' & (i). ToString () & _
' > ' & i.ToString () & ' </a > ')
End If
Next

If Npageend < Npagecount Then
Response.Write (' <a HREF = ' ' & Script_name & _
'? Page= ' & (Npageend + 1). ToString () & _
"> More ... </a >")
End If
End Sub
</script >


The two paging browsing record types described in this article, although the databases used are local, are equally applicable to other types of databases, and this can only be accomplished by modifying the data connection string, for example, if you are using a SQL Server database. This SQL Server database server is ' Server1 ', the database is ' data ', the user name is the default ' sa ', and no password is set. You just need to transform the string ' strconn ' in the above two-part program into:

strconn = ' Provider = sqloledb.1; Persist security Info = False; User ID = sa; Initial Catalog = Data; Data Source = Server1 '


can be achieved.

Six Summarize:

The two paging browsing data logging methods described in this article are very useful in asp.net database programming, because in data processing, pagination display records are more difficult than others, such as data modification and deletion. Hopefully the above content will help you develop database programs using asp.net.

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.