In Microsoft's ASP programming system, the establishment of ADO objects makes it easy to access the database from the web page, especially the ADO recordset object makes the output and display of control data more convenient and free. In visual interdev6.0 (vi6.0), the script Object Model (SOM), design-time control (DTC), and data environment object model (deom) the introduction of object models makes the webpage access to the database more convenient.
For topic reasons, the following sections only provide code and brief comments on database connections, focusing on how to use the recordset object (or control) to display data records on pages. According to my understanding, the key to paging display lies in mastering the attributes and methods of the recordset object of ADO or the recordset control of DTC (design-Time Controller.
The seven types of display weapons are classified into four categories:
The first and second methods are called "Pure ASP method", which is also the most widely used method for ASP websites in China. The difference between them is only the difference in implementation skills. The implementation of these two methods is the easiest to understand, the concepts of objects used are the least, and the requirements for the development environment are also the lowest (as long as the notepad is used ). It can be said that the essence of the two methods is the CGI programming idea, but the ADO object is introduced in the program.
The fourth and fifth types are temporarily named "Som DHTML method ". These two methods require the use of the script Object Model (script object model) proposed by Microsoft in the vi6.0 environment) and the new features of binding table objects to databases in DHTML (many books and articles only introduce the use of dhtml css features in style design while ignoring the introduction of its data binding features ), implement page flip control on the client. However, it requires that your browser support DHTML, such as Microsoft Internet Explorer 4.0 or later.
The sixth type is "Som server 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, and grid on the server (client) implement paging control. This is an exciting and brand-new programming method that treats webpages as objects (this object model is different from the traditional dom-Document Object Model: Dom can only control the client, and SOM can control the server side and client side), it truly implements Object-Oriented Programming for Web pages. Unfortunately, my personal abilities may be limited. I personally think this technology is not very mature. For example, the combination with the browser is not very good, which will be described in detail later.
The seventh type is called the "deom method ". It also uses the data environment object model established in vi6.0 to create A recordset object. This is also a new method that is rare in Web programming. Compared with the SOM model, it has its own advantages, which will be detailed later.
The source code of all the examples mentioned later can be directly copied and used. You don't even know how it works. You just need to replace the rough italic part with your own database name or field name.
Before introducing the paging methods in detail, let's create a database: Use Access in office97 to create an employee. mdb, where an EMP table is created with only three fields: emp id, last name, and first name. The reason is that we are concerned about how to process the recordset results.
First: direct parameter access method
This method is to manually create A recordset object and use its pagesize (the number of records displayed per page is specified), pagecount (total number of pages), and absolutepage (current number of pages) attribute to control the output of pages. Pagination uses the
<% // Establish a connection with the Database "employee. mdb.
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open "driver = {Microsoft Access Driver (*. mdb)}; DBQ = employee. mdb"
// Create A recordset object instance RS for 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") '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> page 1 </a>"
Response. Write "<a href = emp1.asp? Page = "& amp; (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 each page
// Display the 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>"
// Display each record cyclically
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
Next
Response. Write "</table> </center>" %>
Type 2: Form transfer Parameter Method
This method is the same as the first method when creating a recordset object. It only uses the <input> and case statements to achieve page turning when page turning control. The webpage name is emp2.asp. This method has a disadvantage in programming logic: When you press the "Previous Page" or "next page" button and then press the refresh button on the browser, the page is automatically turned over. The source code is as follows:
If pagenum = "" Then pagenum = 1 // displayed on the first page
// Create a database connection and recordset object instance Rs.
This is the same as the first method. It is skipped here.
Rs. pagesize = 10' set the number of records displayed on the page to 10
// Determine the page flip 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 on the current page
// Display the current page
Same as the first method, skipped here.
// Set the nav flip button
<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" name = "nav">
<Input type = "Submit" value = "last" name = "nav"> </form>
Third: Use the Grid Control to design pagination
Among all methods, this method is the easiest. You only need to drag the recordset control and Grid Control in DTC to the ASP Webpage. In addition, you can choose whether to control pages on the server platform or on the client platform. The disadvantage is that you must display the table in the format specified by the table, rather than controlling the display format of the table.
The method is as follows:
Create an EMP. VIP project in vi6.0. Add an ASP Webpage: emp3.asp in the project.
Step 1: Select "add data connect…" on the menu bar of vi6.0 ...", By following the navigation prompts of the development tool, you can easily establish a connection with the database of employee. MDB. Drag a recordset control from the DTC toolbar to the webpage and set its properties. Details
When you drag the control to the webpage, vi6.0 will automatically prompt you "whether to use the scripting object model", and press Yes.
Step 3: drag a grid control from the DTC toolbar to the webpage and right-click it to set its properties. For example, select the recordset Control name created in step 2, select fields in the EMP table, the number of records displayed on each page, and the display format. It is very simple and convenient. Just follow the navigation prompt.
Type 4: DHTML method 1.
Data records are displayed in an HTML table. It uses the data binding feature of tables in DHTML to control the paging display of records. The disadvantage is that your paging method will be restricted to a specific method: You can only "Previous Page" and "next page", but not "Homepage" and "last page ". This and fifth methods are the fastest to control page flip on the client, but unfortunately they can only be used in browsers that support DHTML.
In DHTML, The datasrc attribute of <Table> binds a table to a data source, and datapagesize can specify the number of records displayed on one page.
Let's take a look at the following example:
Step 1: drag the recordsetcontrol to the newly created web page emp4.htm and set its attributes. 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 previously set recordset Control name is recordset1. Five records are displayed on each 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 dataworkflow = "emp id"> </div> </TD> // output table content
<TD> <Div dataworkflow = "last name"> </div> </TD>
<TD> <Div dataworks = "first name"> </div> </TD>
</Tr>
</Table>
Step 3: Add a pair of dtcs button controls for paging navigation. One is named "btnprevious" (Previous Page) and the other is named "btnnext" (next page ). Their scripts are as follows:
<Script language = VBScript>
Function btnprevius_onclick ()
Table1.previouspage ()
End Function
Function btnnext_onclick ()
Table1.nextpage ()
End Function
</SCRIPT>
Category 5: DHTML method 2
This method is perfect for the fourth method. By writing scripts manually, we can make the "Homepage", "Last page" flip navigation buttons, and determine the location (Record Number) of each record ). Due to the length, I will introduce only one specific example below and give a brief description. For other attributes and methods of DHTML and recordset controls, refer to relevant books. Note that the recordset control is somewhat different from the ADO recordset object described in methods 1 and 2: The recordset control does not directly provide attributes such as pagesize and pagecount, we need to use the method described below for calculation.
Step 1: drag the recordsetcontrol file to the newly created web page emp5.htm and set its attribute. The method is the same as the third method.
Step 2: Define three global variables and compile the ondatasetcomplete script for recordset1.
Dim gcurrentpagenumber // current page number
Dim gmaxpagenumber // maximum number of pages
Dim grecordsperpage // number of records displayed on each page
Grecordsperpage = 5 // set the number of records displayed on each page to 5.
Function recordset1_ondatasetcomplete ()
Totalrecordcount = recordset1.getcount () // The total number of records
Gmaxpagenumber = int (totalrecordcount/grecordsperpage) // obtain the maximum number of pages
If (totalrecordcount mod grecordsperpage)> 0 then
Gmaxpagenumber = gmaxpagenumber + 1
End if
End Function
Step 3: Create a flip navigation button.
Function btnfirst_onclick () 'is displayed on the homepage.
Gcurrentpagenumber = 1
Displaydata ()
End Function
Function btnprevius_onclick () 'goes to the previous page
If gcurrentpagenumber> 1 then
Gcurrentpagenumber = gcurrentpagenumber-1
Displaydata ()
End if
End Function
Function btnnext_onclick () 'to the next page
If gcurrentpagenumber <gmaxpagenumber then
Gcurrentpagenumber = gcurrentpagenumber + 1
Displaydata ()
End if
End Function
Function btnlast_onclick () 'goes to the last page
Gcurrentpagenumber = gmaxpagenumber
Displaydata ()
End Function
Step 4: compile a function that displays each page. Many DHTML attributes and methods are used. Please refer to relevant books on your own.
Sub displaydata ()
Startrecord = (gcurrentpagenumber-1) * grecordsperpage) + 1 // calculate the number of records displayed at the beginning of each page (location, number of records)
Rowctr = 1
Lblpagenumber. innerhtml = gcurrentpagenumber & "/" & gmaxpagenumber
For recordptr = startrecord to (startrecord + grecordsperpage-1) // display records on one page cyclically
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 // display each page
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) 'inserts a new column
For cellctr = 0 to 3
Table1.rows (rowctr). insertcell ()
Next
Next
Method 6: server-side paging Control.
If we generate HTML statements by page on the server and then output them to the client, the browser does not support DHTML. However, the server-side method requires that the recordset control be re-generated every time we flip pages. Therefore, the speed must be slower than that of DHTML. However, if the server is fast enough, slow customers will not be aware of it.
In the following example, we will introduce a new DTC control: pageobject. This control makes the specified webpage a webpage object. The subprograms and functions organized by the user in the server script of the webpage can be seen as the webpage object method. It provides an advanced way to manage state information: webpage objects have some attributes (variables) that allow you to define the lifetime of these attributes. Because of these features, it is very convenient for us to compile the paging script.
However, the disadvantage of this method is that when you press the "Previous Page" or "next page" button, and then press the refresh button on the browser, the webpage will automatically flip the page. In addition, if you press the "back" button on the browser and then press the "flip" button, a page flip may occur. This is because of the webpage object attributes (global variables.
Step 1: drag the recordset control to the newly created web page emp6.asp. The name is recordset1 and its attributes are set. The method is the same as the third one, which is omitted here.
Step 2: drag the pageobject control to the webpage and name it emplist. Right-click the control to open the property page and set the maxpagenumber, recordsperpage, and curr?pagenumber attributes (global variables ). Vi6.0 uses get and set methods to read and write their values. For more information about their usage, see.
Step 3: Compile the ondatasetcomplete event of recordset1.
Function recordset1_ondatasetcomplete ()
Recordsperpage = 5
Emplist. setrecordsperpage (recordsperpage) // you can set the number of records on each page of a webpage object to 5.
Totalrecordcount = recordset1.getcount () // obtain the total number of records
MPN = int (totalrecordcount/recordsperpage) // calculate that MPN is 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 webpage and write a page flip control script. We can flip the page by changing the value of the currentpagenumber attribute of the webpage object.
Function btnfirst_onclick () 'is displayed on the homepage.
Emplist. setcurrentpagenumber (1)
End Function
Function btnprevius_onclick () 'goes to the previous page
CPN = emplist. getcurrentpagenumber ()
If CPN> 1 then
Emplist. setcurrentpagenumber (CPN-1)
End if
End Function
Function btnnext_onclick () 'to the next page
CPN = emplist. getcurrentpagenumber ()
If CPN <emplist. getmaxpagenumber () then
Emplist. setcurrentpagenumber (CPn + 1)
End if
End Function
Function btnlast_onclick () 'goes to the last page
Emplist. setcurrentpagenumber (emplist. getmaxpagenumber ())
End Function
To ensure that the first page displays the first page, we have to write the onenter event of the webpage object.
Function emplist_onenter ()
If emplist. firstentered then
Emplist. setcurrentpagenumber (1)
End if
End Function
Step 5: compile a script that displays each page.
<HR> <Table border = 0> <tr> // display the 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 flip, same as the 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>
7. Data Environment Object Model (data environment object model) Method
The data environment object model refers to the ADO object model and its objects-"connection", "command", "recordset ", "field" and "parameter" objects-abstract to a form that is easier. Data Environment object model exposes the command as a method. You can call these methods to execute these commands and return the resulting record set. For more information about the deom object model, see related books. Let's take a look at the example of emp7.asp on the following webpage:
Step 1: In the "Project Explorer" Window of vi6.0, right-click the project and select "add data connection" from the pop-up menu ". After a connection to the database is established according to the navigation prompt provided by 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 on the genetal page in the command1 properties pop-up window and enter select * from EMP. Press OK to return.
Step 3: After you create this data command, you have created a method for the data environment object, and then you can call this method from the script, in addition, this method returns a record set to the user.
Thispage. createde () // In Som mode, thispage indicates the current webpage object, and the createde () method creates the de object.
De. command1 // execute the command of the De object, which can be used later on behalf of parameters for conditional query.
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 implementation paging Code fully references the methods described above, which is skipped here.
Other methods such as those implemented in the database navigation of frontpage2000 are omitted here because they are irrelevant to this topic.
To sum up, each method described above contains many new technologies, which cannot be further explored Due to space limitations. This article is just intended to introduce multiple methods of ASP Web page programming through the specific example of page turning; let everyone experience the powerful functions of vi6.0 in the preparation of web pages; understand and be familiar with the usage of ADO, DHTML, DTC control, SOM object model and deom object model proposed by Microsoft in Web programming; we hope to provide you with more choices and references when preparing your webpage.