Using ASP to realize paging browsing of table

Source: Internet
Author: User
As we all know, ASP has a powerful database operation capability, which is inseparable from her convenient to invoke ActiveX objects. Let me introduce an ASP method of using ADO object to realize paging display of database records. The following code is passed under the win98+pws+msaccess environment.
Here I mainly through the ADO object set of Recordset object to implement a variety of database operations. First, several recordset properties for paging display are introduced.

PageSize: The number of records displayed per page.

PageCount: According to the user set up the pagesize and the total number of records in the table, the system automatically calculates total pages.

RecordCount: The total number of records in the table.

AbsolutePage: Represents the current page number. If you set the AbsolutePage property to 3, the current record moves to page 3rd 1th (that is, 31st).

After seeing the recordset with these attributes, I believe we all feel that it is easy to do a paging display program. Here is a thought, all the code to implement this function is placed in the display.asp, according to the Order of execution is: Open the database and table, read the user requirements of the display, set up a good pagesize and AbsolutePage, output the content to the browser, set a good navigation bar. Here's the code:


<% '---------------------------Open database and table
Set Conn=server.createobject ("ADODB. Connection ")
Filepath=server.mappath ("Abc.mdb") '-------assume that the database file is "Abc.mdb"
Conn. Open "Driver={microsoft Access driver (*.mdb)};d bq=" & filepath
Set Rs=server.createobject ("ADODB. Recordset ")
Rs.Open "main", conn,3,2 '-------assume the name of the table is "main"
%>
<% '---------------------------read the display of user requirements (passed by parameter)
Line=cint (Request ("line"))
Page=cint (Request ("page")
%>
<% '---------------------------Set the display mode according to the user's requirement.
Rs. Pagesize=line
Rs. Absolutepage=page
%>
<% '---------------------------Display content%>
<table width= "100%" border= "1"
<tr>
<%for i=0 to Rs. fields.count-1%>
<td> <%=rs. Fields (i). name%> </td>
<%next%>
</tr> <% '-------The above section shows the header, which is the field name%>
<%for I=1 to Rs. pagesize%>
<tr>
<%for j=0 to Rs. fields.count-1%>
<td> <%=rs. Fields (j). value%> </td>
<%next%>
</tr>
<%rs.movenext%>
<%if rs.eof Then Exit for%>
The <%next%> <% '-------shows the contents of the table in the above section%>
</table>

<% '---------------------------navigation bar%>



<%if page <> 1 then%>
<%="line%"> > first page
<%else%> first page <%end if%>



<%if page>1 then%>
<%="page-1%"> &line= <%=line%> > previous page
<%else%> Previous page <%end if%>


<%if page
<%="page+1%"> &line= <%=line%> > next page
<%else%> Next page <%end if%>


<%if page <> Rs. PageCount then%>
&line= <%=line%> > last page
<%else%> last page <%end if%>
<% '-------all of the above four items need to be judged: if you do not provide the first page and previous page links on the first page
'-------If you do not provide a link to the last page and the next page on the last page%>


Please enter page number:




Please set each page

<% '-------because there is only one form field in all two forms, no Send button is provided, just hit the carriage return line%>

Page <%=page%>/Total <%=rs. pagecount%> page

This is just a basic program, no optimization, there are some bugs, let's make it more "perfect" some of the following.

A About parameter passing. If there are 46 records in the table and then we typed http.//localhost/display.asp?line=10&page=2 in the address bar (assuming this is the correct path), the browser will show us the 10 records for the second page. But if you only typed http.//localhost/display.asp in the address bar, the browser gave us an error message. How to solve it? We must add a judgment when reading these parameters, and if the argument is empty, assign it an initial value to prevent errors. Replace the read user requirements by section with the following code:


<%
If Request ("page") <> "then
Page=cint (Request ("page")
Else
Page=1 '-------If the argument page is empty, assign it a value of 1
End If
If Request ("line") <> "then
Line=cint (Request ("line"))
Else
line=10 '-------If the argument line is empty, assign it a value of 10
End If
%>


Two The rationality problem of the parameter. What would happen if we typed http.//localhost/display.asp?line=20&page=4 in the address bar? The browser complains. Because there are only 46 records in the table, and we want it to show 20 on each page, 20 on page fourth, that is, 61st to 80th record. There's obviously going to be a mistake. The same situation will also appear in the navigation bar, if the current browser is displayed in 10 per page, page Fourth of the content, we go to set it to 20 per page display, but also error. The solution is the same, first judge the rationality of the parameters, and then set the display mode. Replace the following code with the "set the display by user Requirements" section.

<%
If page> (Rs. RECORDCOUNT-1) \line+1 Then
'-------calculated by calculation, the maximum number of pages = (Total records-1) \ Number of rows per page +1
Response.Write ("error!") '-------If the argument is not realistic, output ' error! '
Response.End '-------Termination procedure
End If
Rs. Pagesize=line
Rs. Absolutepage=page

%>


Three Verify the form field. All two input text boxes in the navigation bar must be entered as integers, otherwise there will be an error. We can take advantage of FrontPage's Validation form field feature to finish, here is not much to say.

Four Optimization. So far, there is one drawback to this program. In the database access process, the most time-consuming is the database open and Recordset object, and this program every execution will open the database and set up the Recordset object, greatly reduce the efficiency, once the access volume increases, this program is to value. The solution is to open the database and set up the Recordset object the first time the program is executed, and then return the values in the session as soon as the next execution. Replace the "Open Database and Tables" section with the following code.

The

<%
If not IsObject (session ("Conn")) then
Set Conn=server.createobject ("ADODB. Connection ")
Filepath=server.mappath (" Abc.mdb ")
Conn. Open driver={microsoft Access Driver (*.mdb)};d bq= "& filepath
Set Rs=server.createobject (" ADODB. Recordset ")
Rs.Open" main ", conn,3,2
Set session (" Conn ") =conn
Set Session (" RS ") =rs
Else
Set conn= Session (' conn ')
Set Rs=session ("RS")
End If
% >


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.