In the Microsoft ASP programming system, the establishment of ADO object makes it easy to access the database from the Web page, especially the ADO Recordset object makes the output display of control data more convenient and free. In Visual InterDev6.0 (hereinafter referred to as VI6.0), because of the script Object Model (hereinafter referred to as SOM), design-time Control (hereinafter referred to as DTC), and the Data Environment The introduction of object Model (hereinafter referred to as DEOM) makes the Web page more convenient for database access design.
Because of the topic, the connection to the database, with the following code and brief comments, focuses 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 is to be proficient in the properties and methods of the ADO Recordset object or the DTC (design-time control) of the Recordset control.
These seven kinds of pages display the weapons summed up in four categories:
The first to second kind I called "The Pure ASP Method", this also is the domestic ASP website to use the most method, their difference only in the Realization skill difference. The implementation of these two approaches is most understandable, with the least object concept used, and the minimum requirements for the development environment (as long as Notepad is on the line). It can be said that the essence of these two methods or CGI programming ideas, only in the program introduced ADO object.
The fourth to fifth temporary name is "the DHTML method of Som". These two methods require the use of the Script object model proposed by Microsoft in the context of VI6.0 Model) and new database bindings for table objects in DHTML (many books and articles only describe the use of DHTML's CSS features in style design and ignore their data binding features), enabling the client to control page flipping. However, it requires that the user's browser must be a version that supports DHTML, such as Microsoft Internet Explorer 4.0 and later.
The sixth kind of temporary name is "SOM server-side method". Required to be developed in a VI6.0 environment, it takes advantage of several DTC controls in the script object model proposed by Microsoft: Recordset, PageObject, grid, etc. on the server side (client) for page control. This is an exciting new programming approach that sees Web pages as objects (this object model differs from traditional DOM----Document Object models: The DOM can only control the client, and the SOM can control the server and client). It really implements the object-oriented programming of the Web page. Unfortunately, perhaps my personal ability is limited, this technology I personally think is not very mature, for example, the combination with the browser is not very good, this will be explained in detail later.
The seventh kind of temporary name is called "Deom Law". It also builds the Recordset object using the Data Environment object model established in VI6.0. This is also a relatively rare new method in Web programming, compared to the SOM model, its own advantages, which will be described in detail later.
In the following example of all the examples of source code, you can directly copy the use, you can not even understand its principle, as long as the bold italic part of the corresponding to their own database name or field name.
Before we begin to detail the various paging methods, let's start by creating a database: Create a employee.mdb with access from OFFICE97, with a table EMP, with only three fields: EMP id,last name and first name. Why this is so simple is because we are concerned about how to deal with the results of the recordset.
The first type: Direct method of parameter generation
This is done by manually creating the Recordset object, using its pagesize (specify the number of records per page), PageCount (total number of pages), and AbsolutePage (current page number) properties to control the paging output. Pagination takes the direct page number parameter method to control page flipping. The name of the Web page is emp1.asp. The source code is as follows:
<%//establish a connection to the Employee.mdb database.
Set conn = Server.CreateObject ("ADODB. Connection ")
Conn. Open "Driver={microsoft Access driver (*.mdb)};d Bq=employee.mdb"
Set up a Recordset object instance of the EMP table Rs.
Set rs = Server.CreateObject ("ADODB". Recordset ")
Rs. Open "EMP", Conn, 3
PageSize =//pagesize property specifies the number of record bars to display per page
Page = CLng (Request ("page")) ' string conversion to type long
If Page 1 Then Page = 1
If Page > Rs. PageCount then Page = Rs. PageCount
If Page <> 1 Then
Response.Write " first Page "
Response.Write " prev "
End If
If Page <> Rs. PageCount Then
Response.Write ""
Response.Write " last page "
End If
Response.Write "page:" & page & "/" & Rs. PageCount & ""
Display of each page
Show Table Header
Response.Write "
Response.Write " " & Rs. Fields ("EMP ID"). Name & ""
Response.Write "" & Rs. Fields ("Last Name"). Name & ""
Response.Write "" & Rs. Fields ("First name"). Name & " "
Cycle 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 " " & Rs. Fields ("EMP ID"). Value & ' '
Response.Write "" & Rs. Fields ("First name"). Value & ' '
Response.Write "" & Rs. Fields ("Last Name"). Value & ' '
Rs. MoveNext
If Rs. EOF then Exit for
Next
Response.Write " "%>
The second type: Form transfer parameter method
This method is the same as the first when creating a Recordset object, except that the and case statement mates are used for page flipping when the page is controlled. The name of the webpage is: emp2.asp. This method has a disadvantage in programming logic: It automatically turns the page when you press the Refresh button on the browser after pressing the "up" or "Next" buttons. The source code is as follows:
If Pagenum = "then Pagenum = 1//display starting from the first page
Establish a database connection and a Recordset object instance Rs.
This is the same as the first method, which is skipped here.
Rs. Pagesize = 10 ' Set the number of records displayed on one page to 10
Determine the action of page flipping
Select Case Request ("NAV")
Case ""
Session ("Pagenum") = 1
Case "First" first Record
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 ("Pagenum")//determines the first record number of the current page
Show Current Page
Same as the first method, which is skipped here.
Nav page button settings
Third: Design pagination with a grid control
Of all the methods, this method is the easiest. You simply drag the recordset control in the DTC and the grid control into the ASP Web page. Also, you can choose whether to control paging on the server platform or on the client platform. The downside is that you have to display it in a given format, and you don't have the freedom to control the display format of the table.
Here's how:
Build an engineering EMP.VIP in the VI6.0. Then add an ASP page to the project: Emp3.asp.
First step: Select "Add Data Connect ..." On the VI6.0 menu bar, and you can easily establish a connection to the Employee.mdb database by following the navigation tips of the development tool. Drag a Recordset control from the DTC toolbar into the Web page and set its properties. Specific
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 the second step, select the field in the EMP table, how many records are displayed per page, and the display format. Very simple and convenient, just follow the navigation tips to do it.
The fourth kind: DHTML law one.
Data records are displayed in an HTML table. It uses data-binding attributes of tables in DHTML to control the paging display of records. The downside is that your page-flipping method will be limited to a specific way: only "on page" and "next page" and not "home" and "last". This and fifth method is the fastest because it is in the client control page, but unfortunately it can only be used on browsers that support DHTML.
In DHTML, the Datasrc property of
enables a table to be bound to a data source, and another property datapagesize to specify the number of records to be displayed one page at a time.
Let's take a look at the following example:
The first step: Drag the recordset control to the newly created Web page emp4.htm, set its properties, in the same way as the third, here slightly.
Step Two: Enter the following code:
//assume that the recordset control named Recordset1 is preceded. Each page shows 5 records.
emp ID //Output header
last Name
first Name
//Output table contents
Step three: Then, add a pair of DTCs button controls to do page navigation, one named "Btnprevious" (previous page), and one named "Btnnext" (Next page). Their corresponding script is as follows:
Function Btnprevious_onclick ()
Table1.previouspage ()
End Function
Function Btnnext_onclick ()
Table1.nextpage ()
End Function
The fifth type: DHTML Law II
This method is perfect for the fourth method. Using the method of manual scripting, we can Do "home", "last page" page navigation button, and can determine the location of each record (record number). As a result of the length of the relationship, I would only introduce a specific example below, and give a brief description. Other properties and methods for DHTML and Recordset controls should be used by the reader to refer to the relevant books themselves. It is important to note that the Recordset control is somewhat different from the ADO Recordset object described in the first to second method: The Recordset control does not directly give attributes such as pagesize and PageCount, which need to be computed using the methods described below.
The first step: Drag the recordset control to the newly created Web page emp5.htm, with the name Recordset1, set its properties, method with the third, here slightly.
Step two: Define three global variables and write Recordset1 ondatasetcomplete (when data is set to complete) script.
Dim gcurrentpagenumber//Current page number
Dim gmaxpagenumber//MAX pages
Dim grecordsperpage//page shows the number of records
Grecordsperpage = 5//sets the number of records displayed per page to 5 records.
Function Recordset1_ondatasetcomplete ()
Totalrecordcount = Recordset1.getcount ()//total number of records
Gmaxpagenumber = Int (totalrecordcount/grecordsperpage)//Get maximum number of pages
If (Totalrecordcount Mod grecordsperpage) > 0 Then
Gmaxpagenumber = Gmaxpagenumber + 1
End If
End Function
Step three: Create a page navigation button.
Function Btnfirst_onclick () ' Turn to Home
Gcurrentpagenumber = 1
Displaydata ()
End Function
Function Btnprevious_onclick () ' Turn to the previous page
If Gcurrentpagenumber > 1 Then
Gcurrentpagenumber = GCurrentPageNumber-1
Displaydata ()
End If
End Function
Function Btnnext_onclick () ' Turn to the next page
If Gcurrentpagenumber Gmaxpagenumber Then
Gcurrentpagenumber = Gcurrentpagenumber + 1
Displaydata ()
End If
End Function
Function Btnlast_onclick () ' Turn to the last page
Gcurrentpagenumber = Gmaxpagenumber
Displaydata ()
End Function
Fourth step: Write the function that displays each page. Many of the properties and methods of DHTML are used, so readers should refer to the relevant books themselves.
Sub Displaydata ()
Startrecord = ((gCurrentPageNumber-1) * grecordsperpage) + 1//calculates the record numbers (positions, the first few) displayed on each page
ROWCTR = 1
lblpagenumber.innerhtml = Gcurrentpagenumber & "/" & Gmaxpagenumber
For recordptr = Startrecord to (Startrecord + gRecordsPerPage-1)//loop to display each record of a page
If recordptr > Recordset1.getcount () then//Show empty table
Table1.rows (ROWCTR). Cells (0). InnerHTML = "
"
Table1.rows (ROWCTR). Cells (1). InnerHTML = "
"
Table1.rows (ROWCTR). Cells (2). InnerHTML = "
"
Table1.rows (ROWCTR). Cells (3). InnerHTML = "
"
Else//Show each page exactly
Recordset1.moveabsolute (RECORDPTR)//move record pointer.
EmpID = Recordset1.fields.getValue ("emp ID")
Emplname = Recordset1.fields.getValue ("First name")
Empfname = Recordset1.fields.getValue ("Last Name")
Table1.rows (ROWCTR). Cells (0). InnerText = Recordptr ' Counter
Table1.rows (ROWCTR). Cells (1). InnerText = EmpID
Table1.rows (ROWCTR). Cells (2). InnerText = Emplname
Table1.rows (ROWCTR). Cells (3). InnerText = Empfname
End If
rowctr = rowctr + 1
Next
End Sub
In addition, we need to write the following script in the OnLoad event of the Window object:
For rowctr = 1 to Grecordsperpage
Table1.insertrow (rowctr) ' Inserting a new column
For cellctr = 0 to 3
Table1.rows (rowctr). InsertCell ()
Next
Next
The sixth kind: The server side controls the paging method.
If we page the data on the server side to form an HTML statement and then output it to the client, there is no problem with the browser not supporting DHTML. But the server-side approach makes it possible for the recordset control to be re-generated every time we turn the page, so the speed is certainly slower than the DHTML approach. But if the server is fast enough, this slow customer is unaware.
In the following example, I will introduce a new DTC control: PageObject. This control makes the specified page a Web object, and the subroutines and functions that the user organizes in the server script for this page can be viewed as a method of the Web Page object. It provides an advanced way to manage state information: Web Page objects have properties (variables) that users can define the lifetime of these properties. Because of these features, we make it very convenient to script the page.
However, the disadvantage of this method is: When you press the "page" or "next Page" button, and then press the Refresh button on the browser, the page will automatically page. Also, if you press the rewind button on your browser and then press the page Turn button, you may get a rollover. This is due to Web Page object properties (global variables).
The first step: Drag the recordset control to the newly created Web page emp6.asp, with the name Recordset1, set its properties, method with the third, here slightly.
The second step: Drag the PageObject control to the Web page, named Emplist. Then right-click the control to open the property page and set Maxpagenumber,recordsperpage,currrentpagenumber three properties (global variables). VI6.0 can read and write their values using the get and set methods, please refer to the relevant data for specific usage.
Step three: Write the Recordset1 ondatasetcomplete event.
Function Recordset1_ondatasetcomplete ()
Recordsperpage = 5
Emplist.setrecordsperpage (Recordsperpage)//Set Page object per page The number of record bars property is 5
Totalrecordcount = Recordset1.getcount ()//Get the total number of records in the recordset
MPN = Int (totalrecordcount/recordsperpage)//Calculate MPN as the total number of pages
If (Totalrecordcount Mod recordsperpage) > 0 Then
MPN = MPN + 1
End If
Emplist.setmaxpagenumber (MPN)
End Function
Fourth Step: Drag four button controls into the Web page and write a page control script. We do this primarily by changing the value of the Currentpagenumber property of the Web Page object.
Function Btnfirst_onclick () ' Turn to Home
Emplist.setcurrentpagenumber (1)
End Function
Function Btnprevious_onclick () ' Turn to the previous page
CPN = Emplist.getcurrentpagenumber ()
If CPN > 1 Then
Emplist.setcurrentpagenumber (cpn-1)
End If
End Function
Function Btnnext_onclick () ' Turn to the next page
CPN = Emplist.getcurrentpagenumber ()
If CPN (Emplist.getmaxpagenumber) then
Emplist.setcurrentpagenumber (CPN + 1)
End If
End Function
Function Btnlast_onclick () ' Turn to the last page
Emplist.setcurrentpagenumber (Emplist.getmaxpagenumber ())
End Function
To ensure that the first page is displayed, we have to write the OnEnter event for the Page object.
Function Emplist_onenter ()
If Emplist.firstentered Then
Emplist.setcurrentpagenumber (1)
End If
End Function
Fifth step: Write the script that displays each page.
emp ID
last Name
first Name
<%
PageNumber = Emplist.getcurrentpagenumber ()//calculate the various parameters required for paging, with DHTML Method II
Recordsperpage = Emplist.getrecordsperpage ()
Startrecord = ((pageNumber-1) * recordsperpage) + 1
LastRecord = Recordset1.getcount ()
For recordptr = Startrecord to (Startrecord + recordsPerPage-1)%>
<%if recordset1.eof = True then%>
<%Else%>
<%recordset1.moveabsolute (recordptr)%>
<% If recordptr <= LastRecord then%>
<%=recordptr%>
<%Else%>
<% End If%>
<%=recordset1.fields.getvalue ("emp ID")%>
<%=recordset1.fields.getvalue ("Last Name")%>
<%=recordset1.fields.getvalue ("First name")%>
<%end if%>
<%Next%>
The seventh type: Data Environment Object Model method
The Data Environment Object Model abstracts the ADO object model and its objects----"Connection", "Command", "Recordset", "Field", and "Parameter" objects----abstracted into an easier form. The DATA Environment Object model exposes the command as a method. Users can call these methods, which execute these commands and return the resulting recordset. For more information about the Deom object model, please refer to the relevant books. Let's take a look at the following page emp7.asp example:
First step: Right-click on the project in the Project Explorer window of VI6.0 and select "Add Data Connection" from the popup menu. After establishing a connection to the database based on the navigation hints given by VI, the user adds a data command that implements accessing the database from an ASP application. At the same time, you will see a "Data environment" object under the Global.asa file in the Project Explorer window.
Step Two: Right-click on the "Data Environment" object and select "Add Data Command" from the pop-up menu to add a Command1. Depending on the VI6.0 navigation tip, you can select SQL Statement in the Genetal page of the Command1 Properties Pop-up window, type: SELECT * from EMP. Press OK to return.
Step three: Once you have created this data command, you have created a method for the Environment object, and then you can call the method from the script, and the method will return a recordset to the user.
Thispage.createde ()//In som mode, thispage represents the current Web Page object, and the Createde () method creates a De object.
de.command1//executes the command of the De object, which can later be used to substitute parameters, which is useful for conditional queries.
Set Rs=de.rscommand1//de.rscommand1 makes the RS object exactly the same as an ADO Recordset object.
Fourth step: Because RS is an ADO object, the following implementation of the paging code is fully referenced in the above mentioned methods, here skip.
Other methods, such as those implemented in FrontPage2000 database navigation, are not related to this topic.
In summary, the previous introduction of each method contains a lot of new technology, due to the length of the relationship, can not go deep. This article only wants to introduce the various methods of ASP Web page programming by implementing the specific example of turning pages; Let us experience the powerful functions of VI6.0 in compiling Web pages, and understand and familiarize with the ADO, DHTML, DTC controls, Microsoft's proposed in Web programming, The use of SOM object model and Deom object model; Shanghai men's Hospital, the program is hoping to give you more choices and references when compiling a webpage.
ASP network programming: Seven kinds of weapons that realize pagination display in ASP