Seven ways to implement pagination display in ASP-application techniques

Source: Internet
Author: User
Tags mdb database prev access database

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 of I temporarily named "Pure ASP Method",
This is also the domestic ASP Web site used most of the method, their differences only in the implementation of the different skills. 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 a Recordset object instance RS for the EMP table. Set rs = Server.CreateObject ("ADODB.") Recordset ") Rs.  Open "EMP", conn, 3 PageSize = Ten//pagesize property specifies the number of record bars to display per page = CLng (Request ("Page") ' string is converted to a 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 the </A>  "Response.Write" <a Href=emp1.asp? Page= "& (Page-1) &" > previous page </A>  "End 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>"//per page display//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 ("name"). Name & </TD> </TR>//Loop to display RS for each record. AbsolutePage = page//assigns the page number to the AbsolutePage property to know the first record number of the current page for the 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>"%>


Second: Form transfer parameters Method
This method is the same as the first when creating a Recordset object, except that the and case statements are used in conjunction with the page control to make the page turn. 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//
set up the database connection and the Recordset object instance RS by starting at the first page. This
is the same as the first method, skipped here.
RS. Pagesize = 10 ' Set the number of records displayed on one page is 10
//Determines the action of the paging
Select case Request ("NAV") Case
"" Session
("pagenum") = 1
   case "The" "The" "The" "
pagenum" = 1 case
"Prev" ' Previous record
if session ("Pagenum") 1 then session ("
pagenum") = Session ("Pagenum")-1 "" Next "," Next record
if session ("
P Agenum ") RS. PageCount then session
("Pagenum") = Session ("Pagenum") + 1-"Last
" ' Last record
Sessio N ("pagenum") = RS. PageCount End
Select
RS. AbsolutePage = CLNG (Session ("Pagenum"))//Determine the first record number of the current page
//Show the current page the
same as the first method, skip here.
//Nav page button settings

    
    

    

    

     
   

The third: This method is easiest in all methods of designing pagination
with the grid control. 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:
to build a project EMP.VIP in 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 the navigation tips of the development tools. Drag a Recordset control from the DTC toolbar to the Web page and set its properties. As shown in figure:
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 a 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 per page, and the display format. Very simple and convenient, just follow the navigation tips to do it.
Type 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.
in DHTML,





the Datasrc property enables a table to be bound to one data source, and another property datapagesize specify the number of records to display one page at a time. Let's take a look at the following example: The first step: Drag the Recordset control to the new page emp4.htm, set its properties, in the same way as the third, slightly here. Step Two: Enter the following code:


   
   
    
    //assume the preceding recordset control name is Recordset1. Each page displays 5 records. 
    
    //Output header 
      //Output table contents 
      
emp ID last Name first Name

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:


Fifth: DHTML Method II
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//Max page
Dim grecordsperpage//Per page display record number
Grecordsperpage = 5/ /Set the number of records to display on each page is 5 records.
Function recordset1_ondatasetcomplete ()
totalrecordcount = Recordset1.getcount ()//Total number
of record bars Gmaxpagenumber = Int (totalrecordcount/grecordsperpage)//Get maximum pages
If (totalrecordcount Mod grecordsperpage) > 0 then< C9/>gmaxpagenumber = gmaxpagenumber + 1 end
If End
Function

Step three: Create the paging navigation button.

function Btnfirst_onclick () ' Turn to home
gcurrentpagenumber = 1
displaydata () End
function
function  Btnprevious_onclick () ' Turn to previous page
if Gcurrentpagenumber > 1 Then
gcurrentpagenumber = gCurrentPageNumber-1
Displaydata ()
End If the function
function Btnnext_onclick () ' Turn to 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 to calculate the record numbers (position, ordinal) of each page beginning to appear rowctr = 1 LBLP agenumber.innerhtml = Gcurrentpagenumber & "/" & Gmaxpagenumber for recordptr = Startrecord to (Startrecord + GREC ORDSPERPAGE-1)//loops display the records of one 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//specific display 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 = Empfna Me 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.
Step three: Write the Recordset1 ondatasetcomplete event.

Function recordset1_ondatasetcomplete ()
recordsperpage = 5
emplist.setrecordsperpage (recordsperpage)// Sets the Page object per page Record Bar Number property is 5
totalrecordcount = Recordset1.getcount ()//Gets the total number of recordsets
MPN = Int (Totalrecordcount/ Recordsperpage)//calculates MPN as Total page
if (totalrecordcount Mod recordsperpage) > 0 then
MPN = MPN + 1 End
if
Emplis T.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 Home
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" function function Btnnext_onclick () ' Turn to 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 display for each
One-page script. <HR> <table border=0> <TR>//Show header <th align= "left" width=35> </TH> <th align= "left" width=150>emp ID </TH> <th align= ' left ' WI Dth=200>last Name </TH> <th align= "left" Width=200>first Name </TH> </TR> <% pagenumber = Emplist.getcurrentpagenumber ()// Calculate the various parameters required for paging, with the DHTML method two recordsperpage = Emplist.getrecordsperpage () Startrecord = ((pageNumber-1) * recordsperpage) + 1 LA  Strecord = Recordset1.getcount () for recordptr = Startrecord to (Startrecord + recordsPerPage-1)%> <%if = 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.get Value ("emp ID")%> </TD> <TD> <%=recordset1.fields.getvalue ("Last Name")%> </TD> <TD> <%=recordset1.fields.getvalue (" The ' name ')%> </TD> </TR> <%end if%> <%Next%> </TABLE> <HR>
 

Seventh: The Data Environment object Model (Data Environment objects models) method
Data Environment object model to----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 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 look at the following page emp7.asp example:
First step: 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 the "Data Environment" object and select the "Add Data Command" option 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. The
de.command1//executes the commands of the DE object, followed by a surrogate argument, which is useful when you make conditional queries. The
Set Rs=de.rscommand1//de.rscommand1 makes the RS object fully equivalent to an ADO Recordset object.

Step Fourth: Because RS is an ADO object, the following implementation page-flipping code fully references several of the methods described above, skipped here.
Other methods, such as those implemented in FrontPage2000 database navigation, are not related to this topic.
In summary, each of the methods described earlier contains a number of new technologies that cannot be deepened due to the length of the relationship. 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.

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.