Seven kinds of weapons in ASP to realize paging display

Source: Internet
Author: User
Tags format end new features object model prev access database client access
Pagination | Paging in Microsoft's ASP programming system, the establishment of ADO object makes it easy to access database from Web page, especially ADO Recordset object makes the output of control data display more convenient and free. In Visual InterDev6.0 (hereinafter referred to as VI6.0), because of the script Object Model (SOM), design-time Control (hereinafter referred to as DTC), and Data Environment Object Model (hereinafter referred to as DEOM), such as the introduction of such objects, so that the Web page access to the database design seems more convenient.

For the purposes of the subject, for the connection to the database, only code and brief comments are given below, focusing on how the Recordset object (or control) can be used to implement the paging display of data records. As I understand it, the key to paging display is the mastery of the properties and methods of the Recordset control of the ADO Recordset object or DTC (design-time control).

These seven types of page-showing weapons are summed up in four categories:

The first to second kind I temporarily named "Pure ASP Method", this is also the domestic ASP website uses the most method, their difference only in realizes the skill difference. The implementation of these two methods is the easiest to understand, the object concept is the least, the development environment is the minimum requirements (as long as Notepad). It can be said that the essence of these two methods or CGI programming ideas, but only in the program to introduce the ADO object.

The fourth to fifth kind is called "Som's DHTML Method". Both of these methods require the Scripting object model proposed by Microsoft in the context of VI6.0 (script objects Model) and the new features of the database binding to the Table object in DHTML (many books and articles only introduce the use of DHTML CSS features in style design without introducing their data-binding characteristics), and enable the paging on the client control. However, it requires that the user's browser must support DHTML, such as Microsoft Internet Explorer version 4.0 and above.

The sixth kind is named "Som server-side method". Required to be developed in a VI6.0 environment, it utilizes several DTC controls in the Scripting object model presented by Microsoft: Recordset, PageObject, grid, and so on on the server side (the client) to achieve page control. This is an exciting, new programming approach that looks at web pages as objects (this object model differs from traditional DOM----Document Object models: The DOM can only control the client, while the SOM controls the server side and the client), It truly implements the Object-oriented programming of the Web page. But unfortunately, perhaps my personal ability is limited, this kind of technology I personally think is not very mature, for example, the combination with the browser is not very good, this will be detailed in the later text.

The seventh kind of temporary name is called "Deom Law". It also builds the Recordset object using the Data Environment Object Model (VI6.0) established in the environment. This is also a relatively rare new method of Web programming, compared to the SOM model, has its own advantages, which will be described in detail later.

In the following all examples of the source code, can be directly copied to use, you can not even understand its principle, as long as the Bold italic character part of the corresponding to their own database name or field name on it.

Before we begin to detail the various paging methods, let's create a database: Create a employee.mdb with access in Office97, build a table EMP, set only three fields: EMP id,last name and first name. Why this is so simple, because we care about how to deal with the results of the recordset.

The first: Direct method of parameter generation

This is done by manually creating the Recordset object, using its pagesize (specify the number of records to be displayed per page), PageCount (total number of pages), and the AbsolutePage (current page number) property to control the paging output. Pagination uses the
<%//establishes a connection to the Employee.mdb database.
Set conn = Server.CreateObject ("ADODB. Connection ")
Conn. Open "Driver={microsoft Access driver (*.mdb)};d Bq=employee.mdb"
Create the Recordset object instance RS for the EMP table.
Set rs = Server.CreateObject ("ADODB.") Recordset ")
Rs. Open "EMP", Conn, 3

PageSize = The//pagesize property specifies the number of record bars to display per page
Page = CLng (Request ("Page")) ' String type to Long
If Page 1 Then page = 1
If Page > Rs. PageCount Then Page = Rs. PageCount
If Page <> 1 Then
Response.Write "<a Href=emp1.asp?" page=1> the first page of </A> 
Response.Write "<a Href=emp1.asp?" Page= "& (Page-1) &" > prev </A> 
End If
If Page <> Rs. PageCount Then
Response.Write "<a Href=emp1.asp?" Page= "& (page+1) &" "Next page </A> "
Response.Write "<a Href=emp1.asp?" Page= "&rs. PageCount & "> Last page </A> "
End If
Response.Write "page:" & page & "/" & Rs. PageCount & "</font>"
The display of each page
Show Table Headers
Response.Write "<CENTER> <table border=1>"
Response.Write "<TR> <TD>" & Rs. Fields ("emp ID"). Name & "</TD>"
Response.Write "<TD>" & Rs. Fields ("Last Name"). Name & "</TD>"
Response.Write "<TD>" & Rs. Fields ("name"). Name & "</TD> </TR>"
Looping through each record
Rs. AbsolutePage = page//Assign page number to AbsolutePage property to know the first record number of the current page
For ipage = 1 to Rs. PageSize//
Response.Write "<TR> <TD>" & Rs. Fields ("emp ID"). Value & "</TD>"
Response.Write "<TD>" & Rs. Fields ("name"). Value & "</TD>"
Response.Write "<TD>" & Rs. Fields ("Last Name"). Value & "</TD> </TR>"
Rs. MoveNext
If Rs. EOF Then Exit for
Next
Response.Write "</TABLE> </CENTER>"%>
The second type: Form transfer parameter method

This method is the same as the first when creating a Recordset object, except that when the page is controlled, the page is implemented with <input> and case statements. The name of the Web page is: emp2.asp. This approach has the disadvantage of programming logic: When you press the "prev" or "Next" button, and then press the Refresh button on the browser, the page will automatically turn. The source code is as follows:

If Pagenum = "" Then pagenum = 1//Starting from first page
Establish the database connection and the Recordset object instance Rs.
This is the same as the first method, skipped here.

Rs. Pagesize = 10 ' Set the number of records displayed on one page is 10 bar
Determine the action of the page
Select case Request ("NAV")
Case ""
Session ("Pagenum") = 1
Case "A", "the"
Session ("Pagenum") = 1
Case "Prev" ' Previous record
If session ("Pagenum") > 1 Then
Session ("Pagenum") = Session ("Pagenum")-1
End If
Case "Next" ' Next record '
If session ("Pagenum"), RS. PageCount Then
Session ("Pagenum") = Session ("Pagenum") + 1
End If
Case "Last" ' last record '
Session ("pagenum") = RS. PageCount
End Select
Rs. AbsolutePage = CLNG (Session ("Pagenum"))//Determine the first record number of the current page
Show Current Page
With the first method, skip here.
Nav page button settings
<form method= "Get" action= "emp2.asp"
<input type= "Submit" Name= "NAV" value= "Home" >
<input type= "Submit" value= "on page" Name= "NAV"
<input type= "Submit" value= "next page" Name= "NAV"
<input type= "Submit" value= "last" Name= "NAV" > </form>
Third: Design pagination with Grid control

Of all the methods, this method is the easiest. All you need to do is drag the recordset control and the grid control from the DTC to the ASP Web page. Also, you can choose whether to control the paging on the server platform or on the client platform. The disadvantage is that you have to display it in a given format, but you can't control the display format of the table yourself.

The method is as follows:

Build a project EMP.VIP in the VI6.0. Then add an ASP page to the project: Emp3.asp.

Step One: Select "Add Data Connect ..." On the VI6.0 menu bar, and you can easily establish a connection to the Employee.mdb database by the navigation tips of the development tools. Drag a Recordset control from the DTC toolbar to the Web page and set its properties. Detailed diagram:

When you drag a control to a Web page, VI6.0 automatically prompts you to "Use scripting Object Model" and press Yes.

Step three: Drag a Grid control from the DTC toolbar to the Web page, and then right-click to set its properties, such as the name of the recordset control you created in step two, and select Em



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.