Seven ways to implement paging display in MS Visual InterDev6.0
When we compile database-related Web pages, because of the large amount of data to be displayed, we often face a problem of paging of data records.
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).
The seven ways of paging 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.
First: The direct generation of parameters.
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> first Page </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 </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 approach is the same as the first when creating a Recordset object, except that the <input> and case statements are used in conjunction with the paging control. 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= "prev" 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, select the fields in the EMP table, how many records to display on each page, and the display format. Very simple and convenient, just follow the navigation tips to do it. As shown in figure:
Fourth: DHTML method One.
The data record is displayed in an HTML table. It uses the data-binding attributes of the tables in DHTML to control the paging display of records. The disadvantage is that your page-flipping method will be limited to a specific way: only "page" and "Next" and not "first" and "last". This and the fifth method is the fastest, but unfortunately it can only be used on a DHTML-enabled browser because it is paging on the client side.
The DATASRC attribute,<table> in DHTML enables the table to be bound to one data source, and another property datapagesize to specify the number of records to display once a page.
Let's look at the following example:
The first step: Drag the Recordset control to the new page emp4.htm, set its properties, the same as the third, which is abbreviated here.
Step Two: Enter the following code:
<table id= "Table1" datasrc= "#Recordset1_RDS" datapagesize=5>//assume the preceding recordset control name is Recordset1. Each page displays 5 records.
<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 contents
<td><div datafld= "Last Name" ></DIV></TD>
<td><div datafld= "A-Name" ></DIV></TD>
</TR>
</TABLE>
Step three: Then, add a pair of DTCs button buttons to make the page navigation, one named "Btnprevious" (previous), and one named "Btnnext" (next). Their corresponding script is as follows:
<script language=vbscript>
Function Btnprevious_onclick ()
Table1.previouspage ()
End Function
Function Btnnext_onclick ()
Table1.nextpage ()
End Function
</SCRIPT>
Fifth: DHTML method Two.
This method is perfect for the fourth method. The manual scripting method allows us to do the "first page", "last Page" page navigation buttons, and to determine the location of each record (record number). Due to the length of the relationship, I would only introduce a specific example below, and give a brief explanation. Other properties and methods for DHTML and Recordset controls ask the reader to refer to the relevant books themselves. Note here that the Recordset control differs somewhat from the ADO Recordset object described in the first to second method: The Recordset control does not directly give properties such as pagesize and PageCount, and it needs to be evaluated using the method described below.
The first step: Drag the Recordset control to the new page emp5.htm, the name is Recordset1, set its properties, the same as the third, here slightly.
Step two: Define three global variables and write Recordset1 ondatasetcomplete (when data settings are complete) script.
Dim gcurrentpagenumber//Current page number
Dim gmaxpagenumber//maximum pages
Dim grecordsperpage//per page display record number
Grecordsperpage = 5//Set record number of 5 records per page.
Function Recordset1_ondatasetcomplete ()
Totalrecordcount = Recordset1.getcount ()//total number of record bars
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 the paging navigation button.
Function Btnfirst_onclick () ' Turn to homepage
Gcurrentpagenumber = 1
Displaydata ()
End Function
Function Btnprevious_onclick () ' Turn to 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 Last
Gcurrentpagenumber = Gmaxpagenumber
Displaydata ()
End Function
Step Fourth: Write a function that displays each page. Using many of the properties and methods of DHTML, readers should refer to the books themselves.
Sub Displaydata ()
Startrecord = ((gCurrentPageNumber-1) * grecordsperpage) + 1 calculate the record numbers (position, ordinal) at which each page begins to appear
ROWCTR = 1
lblpagenumber.innerhtml = Gcurrentpagenumber & "/" & Gmaxpagenumber
For recordptr = Startrecord to (Startrecord + gRecordsPerPage-1)//loop to display the records of a page
If recordptr > Recordset1.getcount () Then//Show 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//specific display of each page
Recordset1.moveabsolute (RECORDPTR)//move record pointer.
EmpID = Recordset1.fields.getValue ("emp ID")
Emplname = Recordset1.fields.getValue ("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 for 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: server-side control of the page-flipping method.
If we page the data to the server side and then export the HTML statement to the client, there is no problem with the browser not supporting DHTML. But using the server-side method allows us to recreate the recordset control every time we flip the page, so the speed is certainly slower than the DHTML approach. But if the server is fast enough, this slow customer is not aware of it.
In the following example, I'll introduce a new DTC control: PageObject. This control makes the specified Web page an object, and the subroutine and function that the user organizes in the server script of the Web page can be viewed as a method of the Web object. It provides an advanced way to manage state information: Web objects have properties (variables) that allow users to define the lifetime of these properties. Because of the above features, we are very handy when compiling a page-flipping script.
But the disadvantage of this method is: When you press the "prev" or "Next" button, and then the browser press the Refresh button, the page will automatically turn the pages. In addition, if you press the "fallback" button on the browser, and then press the page button, there may be a random turn. This is due to Web Page object attributes (global variables).
The first step: Drag the Recordset control to the new page emp6.asp, the name is Recordset1, set its properties, the same as the third, here slightly.
Step two: Drag PageObject control to the Web page, named Emplist. Then right click on the control to open the property page and set the Maxpagenumber,recordsperpage,currrentpagenumber three properties (global variables). VI6.0 can use get and set methods to read and write their values, please refer to the relevant information. As shown in figure:
Step three: Write the Recordset1 ondatasetcomplete event.
Function Recordset1_ondatasetcomplete ()
Recordsperpage = 5
Emplist.setrecordsperpage (recordsperpage)/Set Page object per page Record Bar Number property is 5
Totalrecordcount = Recordset1.getcount ()//Get total number of recordsets
MPN = Int (totalrecordcount/recordsperpage)//calculates MPN as total number of pages
If (Totalrecordcount Mod recordsperpage) > 0 Then
MPN = MPN + 1
End If
Emplist.setmaxpagenumber (MPN)
End Function
Step Fourth: Drag four button controls to the Web page, and write a paging control script. We do this by changing the value of the Currentpagenumber property of the Page object.
Function Btnfirst_onclick () ' Turn to homepage
Emplist.setcurrentpagenumber (1)
End Function
Function Btnprevious_onclick () ' Turn to 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 Last
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
Step Fifth: Write a script that displays each page.
<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 ()//To calculate the various parameters required for paging, same as 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%>
<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 ("name")%></td>
</TR>
<%end if%>
<%Next%>
</TABLE><HR>
The seventh type is: Data Environment object Model (the method of environment objects).
The Data Environment Object Model abstracts the ADO object model and its objects----"Connection", "Command", "Recordset", "Field", and "Parameter" objects----into a more easily accessible form. The DATA Environment Object model exposes the command as a method. Users can invoke these methods, which execute the commands and return the resulting recordset. For more information on the Deom object model, please refer to the relevant books. Let's take a look at the following page emp7.asp example:
Step One: Right-click on the project in VI6.0 's Project Explorer window and choose Add Data Connection from the pop-up menu. After establishing a connection to the database based on the navigation hints given by VI, the user adds a data command that implements the access to 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 Two: Right click on the "Data Environment" object and select "Add Data Command" from the pop-up menu to add a data command Command1. Depending on the VI6.0 navigation tips, 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 three: After you create 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 the De object.
de.command1//executes the commands of the De object, which can be used as a substitute for arguments, which are useful when you make conditional queries.
The Set Rs=de.rscommand1//de.rscommand1 makes the RS object completely equivalent to an ADO Recordset object.
Fourth step: Because RS for ADO object, so, the following implementation of the paging code completely refer to the several methods described above, skip here.
Other methods, such as FrontPage2000, are implemented in database navigation, which is irrelevant to this topic.
To sum up, each of the methods described earlier contains a lot of new technology, due to the length of the relationship can not be in-depth. This article just wants to introduce the various methods of ASP Web page programming by implementing the specific example of turning page; Let everyone experience the powerful features of VI6.0 in the development of Web pages, understanding and familiarity with the ADO, DHTML, DTC controls proposed by Microsoft in Web programming, The usage of SOM object model and Deom object model; I hope to give you more choices and references in the preparation of web pages.
Reference documents:
1. "Easy mastery of Visual InterDev 6" [Mei]l.michael Van hoozer,jr. Electronics Publishing House
The following three articles are from Microsoft website: http://www.mircosoft.com/vinterdev/
2. "The Visual InterDev 6 Data Environment Object" by Ian Blackburn
3. "Displaying information in sequential Pages with Visual InterDev" by Mike Pope
4. "The Visual InterDev 6_0 scripting Object Model Event Sequence by Steve Millet
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.