In Microsoft's ASP programming system, the establishment of ADO objects makes it easy to access databases from web pages. In particular, ADO's Recordset object makes it more convenient and free to control the output and display of data. In Visual InterDev6.0 (hereinafter referred to as VI6.0), due to the introduction of object models such as Script Object Model (hereinafter referred to as SOM), Design-Time Control (hereinafter referred to as DTC) and Data Environment Object Model (hereinafter referred to as DEOM), This makes the design of web page access to the database more convenient.
Because of the subject matter, regarding the database connection, only the code and brief comments are given below, and the focus is on how to use the Recordset object (or control) to realize the paging display of data records. According to my understanding, the key to paging display lies in the proficiency in the properties and methods of the Recordset object of ADO or the Recordset control of DTC (design time control).
These seven types of weapons displayed in pages can be summarized into four categories:
I temporarily call the first and second methods the pure ASP method, which is also the most commonly used method on domestic ASP websites. The difference between them is only in the implementation techniques. The implementation of these two methods is the easiest to understand, uses the least object concepts, and has the lowest requirements for the development environment (just Notepad will do). It can be said that the essence of these two methods is still the CGI programming idea, but the ADO object is introduced into the program.
The fourth and fifth DHTML methods are temporarily named SOM. These two methods require the use of the Script Object Model proposed by Microsoft and the new database-binding features of the Table object in DHTML in the VI6.0 environment (many books and articles only introduce the CSS features of DHTML It is used in style design and ignores the introduction of its data binding features) to control page turning on the client side. But it requires that the user's browser must support DHTML, such as Microsoft Internet Explorer 4.0 and above.
The sixth method is temporarily named SOM server-side method. It is required to be developed in the VI6.0 environment. It uses several DTC controls in the Script Object Model proposed by Microsoft: Recordset, PageObject, Grid, etc. to implement page turning control on the server side (client). This is an exciting, new programming method that treats web pages as objects (this object model is different from the traditional DOM-document object model: DOM can only control the client, while SOM can Control server and client), it truly realizes object-oriented programming of web pages. But unfortunately, maybe because of my limited personal ability, I personally think that this technology is not very mature yet. For example, the integration with the browser is not very good, which will be explained in detail later.
The seventh method is temporarily named DEOM method. It also uses the Data Environment Object Model established in VI6.0 to create Recordset objects. This is also a relatively new method in web programming. Compared with the SOM model, it has its own advantages, which will be detailed later.
All the example source codes cited below can be copied and used directly. You don’t even need to understand the principles. Just replace the bold and italic parts with the corresponding database names or field names.
Before starting to introduce various paging methods in detail, let us first create a database: use access in Office97 to create an Employee.mdb, create a table emp in it, and set only three fields: emp ID, last name and first name. Why it's so simple is because we care about how to process the results of the recordset.
The first method: direct parameter substitution method
This method is to manually create a Recordset object and use its pagesize (specified number of displayed records per page), pagecount (total page number) and absolutepage (current page number) properties to control paging output. Paging uses <href> to directly take the page number parameter to control page turning. The name of the web page is emp1.asp. The source code is as follows:
<%//Establish a connection with the employee.mdb database.
Set conn = Server.CreateObject(ADODB.Connection)
conn.Open driver={Microsoft Access Driver (*.mdb)};dbq=employee.mdb
//Create the Recordset object instance rs of the emp table.
Set rs = Server.CreateObject(ADODB.Recordset)
rs.Open emp, conn, 3
PageSize = 10 //The pagesize attribute specifies the number of records to be displayed on each page
Page = CLng(Request(Page)) 'Convert string type to long type
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>First page</A>
Response.Write <A HREF=emp1.asp?Page= & (Page-1) & >Previous page</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 number: & Page & / & rs.PageCount & </font>
//Display of each page
//display header
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(first name).Name & </TD></TR>
//Loop to display each record
rs.AbsolutePage = Page //Assign the page number to the absolutepage attribute 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(first 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 transmission parameter method
This method is the same as the first method when creating a Recordset object, except that when controlling page turning, <input> and case statement are used to realize page turning. The name of the web page is: emp2.asp. This method has a shortcoming in programming logic: after pressing the previous page or next page button, and then pressing the refresh button on the browser, the page will turn automatically. The source code is as follows:
if Pagenum = Then Pagenum = 1 //Start displaying from the first page
//Establish a database connection and Recordset object instance rs.
Same as the first method, skipped here.
RS.Pagesize = 10 'Set the number of records displayed on one page to 10
// Determine the page turning action
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(session(Pagenum)) //Determine the first record number of the current page
//Display current page
Same as the first method, skipped here.
// Nav page button settings
<form method=GET action=emp2.asp>
<input type=submit name=NAV Value=Homepage>
<input type=submit value=previous page name=NAV>
<input type=submit value=next page name=NAV>
<input type=submit value=last page name=NAV></form>
The third type: Use Grid control to design paging
Of all the methods, this is the easiest. You only need to drag the Recordset control and Grid control in the DTC to the asp web page. Moreover, you can also choose whether to control page turning on the server platform or the client platform. The disadvantage is that you must display it in the format given by it, and you cannot freely control the display format of the table yourself.
Here's how:
Create a project emp.vip in VI6.0. Then add an asp web page to the project: emp3.asp.
Step 1: Select add data connect... on the menu bar of VI6.0 and follow the navigation prompts of the development tool. You can easily establish a connection with the Employee.mdb database. Drag a Recordset control from the DTC toolbar to the web page and set its properties. As shown in the figure:
When you drag the control to the web page, VI6.0 will automatically prompt you whether to use the Scripting object model, press yes.
Step 3: Drag a Grid control from the DTC toolbar to the web page, then right-click the mouse and set its properties, such as: select the name of the Recordset control created in the second step, select the fields in the emp table, each page How many records are displayed and the display format, etc. It's very simple and convenient, just follow the navigation prompts.
The fourth method: DHTML method one.
Data records are displayed in an HTML table. It uses the data binding features of tables in DHTML to control the paging display of records. The disadvantage is that your page turning method will be limited to a specific way: only the previous and next pages but not the first and last pages. Since page turning is controlled on the client side, this and the fifth method are the fastest, but unfortunately they can only be used on browsers that support DHTML.
In DHTML, the DATASRC attribute of <TABLE> enables the table to be bound to a data source, and another attribute, DATAPAGESIZE, specifies the number of records displayed on a page at one time.
Let's look at the following example:
Step 1: Drag the Recordset control to the newly created web page emp4.htm and set its properties. The method is the same as the third method, which is omitted here.
Step 2: Enter the following code:
<TABLE ID=Table1 DATASRC=#Recordset1_RDS DATAPAGESIZE=5> //Assume that the Recordset control is named Recordset1 earlier. Display 5 records per page.
<THEAD>
<TH ALIGN=left WIDTH=150>Emp ID</TH> //Output header
<TH ALIGN=left WIDTH=200>Last Name</TH>
<TH ALIGN=left WIDTH=200>First Name</TH>
</THEAD>
<TR>
<TD><DIV DATAFLD=Emp ID></DIV></TD> //Output table content
<TD><DIV DATAFLD=Last Name></DIV></TD>
<TD><DIV DATAFLD=First Name></DIV></TD>
</TR>
</TABLE>
Step 3: Then, add a pair of DTCs Button button controls for page navigation, one named btnPrevious (previous page) and the other named btnNext (next page). Their corresponding scripts are as follows:
<SCRIPT LANGUAGE=VBScript>
Function btnPrevious_onclick()
Table1.previousPage()
End Function
Function btnNext_onclick()
Table1.nextPage()
End Function
</SCRIPT>
The fifth method: DHTML method two
This method is a refinement of the fourth method. Using the method of manually writing scripts, we can make the home page and last page navigation buttons, and determine the position (record number) of each record. Due to space constraints, I will only introduce a specific example below and give a brief explanation. For other properties and methods about DHTML and Recordset controls, please refer to relevant books. What needs to be noted here is that the Recordset control is somewhat different from the ADO Recordset object introduced in the first and second methods: the Recordset control does not directly provide properties such as pagesize and pagecount, and needs to be calculated using the method described below.
Step 1: Drag the Recordset control to the newly created web page emp5.htm, name it Recordset1, and set its properties. The method is the same as the third method, which is omitted here.
Step 2: Define three global variables and write the ondatasetcomplete (data setting is completed) script of Recordset1.
Dim gCurrentPageNumber //Current page number
Dim gMaxPageNumber //Maximum number of pages
Dim gRecordsPerPage //Display the number of records per page
gRecordsPerPage = 5 //Set 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 the maximum number of pages
If (totalRecordCount Mod gRecordsPerPage) > 0 then
gMaxPageNumber = gMaxPageNumber + 1
End If
End Function
Step 3: Create page navigation buttons.
Function btnFirst_onclick() 'Turn to the homepage
gCurrentPageNumber = 1
DisplayData()
End Function
Function btnPrevious_onclick() 'Go to the previous page
if gCurrentPageNumber > 1 Then
gCurrentPageNumber = gCurrentPageNumber - 1
DisplayData()
End If
End Function
Function btnNext_onclick() 'Go 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
Step 4: Write a function to display each page. Many DHTML attributes and methods are used. Readers are advised to refer to relevant books.
SubDisplayData()
startRecord = ((gCurrentPageNumber - 1) * gRecordsPerPage) + 1 //Calculate the number of records (position, number) displayed at the beginning of 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 //Display empty table
Table1.rows(rowCtr).cells(0).innerHTML = <P> </P>
Table1.rows(rowCtr).cells(1).innerHTML = <P> </P>
Table1.rows(rowCtr).cells(2).innerHTML = <P> </P>
Table1.rows(rowCtr).cells(3).innerHTML = <P> </P>
Else //Show each page specifically
Recordset1.moveAbsolute(recordPtr) //Move the 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 also need to write the following script in the onload event of the window object:
For rowCtr = 1 to gRecordsPerPage
Table1.insertRow(rowCtr) 'Insert a new column
For cellCtr = 0 to 3
Table1.rows(rowCtr).insertCell()
Next
Next
Sixth method: Server-side control page turning method.
If we paginate the data on the server side to form an HTML statement and then output it to the client, there will be no problem that the browser does not support DHTML. However, using the server-side method requires us to regenerate the Recordset control every time we turn the page, so the speed is definitely slower than using the DHTML method. But if the server is fast enough, this slow client will not notice it.
In the following example, I will introduce a new DTC control: PageObject. This control makes the specified web page become a web page object, and the subroutines and functions organized by the user in the server script of this web page can be regarded as the methods of the web page object. It provides an advanced method of managing state information: web page objects have some properties (variables), and users can define the lifetime of these properties. Because of the above characteristics, it is very convenient for us to compile page turning scripts.
But the disadvantage of this method is: after you press the previous page or next page button, and then press the refresh button on the browser, the web page will automatically turn. In addition, if you press the back button on the browser and then press the page turning button, a random page may occur. This is all caused by the web page object properties (global variables).
Step 1: Drag the Recordset control to the newly created web page emp6.asp, name it Recordset1, and set its properties. The method is the same as the third method, which is omitted here.
Step 2: Drag the PageObject control to the web page and name it emplist. Then right-click this control to open the property page and set the three properties (global variables) of MaxPageNumber, RecordsPerPage, and CurrentPageNumber. VI6.0 can use the get and set methods to read and write their values. Please refer to the relevant information for specific usage.
Step 3: Write the ondatasetcomplete event of Recordset1.
Function Recordset1_ondatasetcomplete()
recordsPerPage = 5
empList.setRecordsPerPage(recordsPerPage)//Set the number of records per page attribute of the web page object to 5
totalRecordCount = Recordset1.getCount()//Get the total number of records in the record set
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
Step 4: Drag four button controls to the web page and write a page turning control script. We mainly achieve page turning by changing the value of the CurrentPageNumber property of the web page object.
Function btnFirst_onclick() 'Turn to the homepage
empList.setCurrentPageNumber(1)
End Function
Function btnPrevious_onclick() 'Go to the previous page
cpn = empList.getCurrentPageNumber()
if cpn > 1 Then
empList.setCurrentPageNumber(cpn - 1)
End If
End Function
Function btnNext_onclick() 'Go 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
In order to ensure that the first page is displayed when entering the page for the first time, we must also write the onEnter event of the web page object.
Function empList_onEnter()
If empList.firstEntered Then
empList.setCurrentPageNumber(1)
End If
End Function
Step 5: Write a script that displays each page.
<HR><TABLE BORDER=0><TR>//Display header
<TH ALIGN=left WIDTH=35></TH>
<TH ALIGN=left WIDTH=150>Emp ID</TH>
<TH ALIGN=left WIDTH=200>Last Name</TH>
<TH ALIGN=left WIDTH=200>First Name</TH></TR>
<%
pageNumber = empList.getCurrentPageNumber()//Calculate various parameters required for page turning, same as DHTML method 2
recordsPerPage = empList.getRecordsPerPage()
startRecord = ((pageNumber - 1) * recordsPerPage) + 1
lastRecord = Recordset1.getCount()
For recordPtr = startRecord To (startRecord + recordsPerPage - 1)%>
<%If Recordset1.EOF = True Then%>
<TR>
<TD> </TD>
<TD> </TD>
<TD> </TD>
<TD> </TD>
</TR>
<%Else%>
<%Recordset1.moveAbsolute(recordPtr)%>
<TR>
<% If recordPtr <= lastRecord Then %>
<TD><%=recordptr%></TD>
<%Else%>
<TD> </TD>
<%End If %>
<TD><%=Recordset1.fields.getValue(emp ID)%></TD>
<TD><%=Recordset1.fields.getValue(last name)%></TD>
<TD><%=Recordset1.fields.getValue(first name)%></TD>
</TR>
<%End If%>
<%Next%>
</TABLE><HR>
Seventh method: Data Environment Object Model (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—into a simpler form. The Data Environment Object Model exposes commands as methods. The user can call these methods, which execute the commands and return the resulting recordset. For detailed information about the DEOM object model, please refer to relevant books. Let’s look at the example of the following web page emp7.asp:
Step 1: Right-click the project in the project Explorer window of VI6.0 and select Add Data Connection from the pop-up menu. After establishing a connection to the database according to the navigation prompts given by the VI, the user adds a data command to access the database from the ASP application. At the same time, you will see a Data Environment object under the global.asa file in the Project Explorer window.
Step 2: Right-click the Data Environment object and select the Add Data Command option from the pop-up menu to add a data command Command1. According to the navigation prompt of VI6.0, you can select SQL Statement in the Genetal page of the Command1 Properties pop-up window and enter: select * from emp. Press OK to return.
Step 3: After you create the data command, you have created a method for the Data Environment object. You can then call this 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 the DE object.
DE.Command1//Execute the command of the DE object, which can be followed by parameters, which is very useful when doing conditional queries.
Set rs=DE.rsCommand1//DE.rscommand1 makes the rs object completely equivalent to an ADO Recordset object.
Step 4: Because rs is an ADO object, the following page turning code completely refers to the several methods introduced above and is skipped here.
Others include methods implemented in the database navigation of FrontPage2000, etc. Since they are not related to this topic, they are omitted here.
To sum up, each method introduced previously contains many new technologies, which cannot be discussed in depth due to space constraints. This article just wants to introduce various methods of ASP web page programming through the specific example of page turning; let everyone experience the powerful function of VI6.0 in preparing web pages; understand and become familiar with ADO, How to use DHTML, DTC controls, SOM object model and DEOM object model; I hope it can provide you with more choices and references when compiling web pages.