Introduction to ASP Programming (20): The paging program for ADO components

Source: Internet
Author: User
Tags add time exit include sql
Ado| Programming | program | Pagination |ado| pagination should say, learned how to insert records, how to display records, then now simple complete article system, news system and message system is not a problem. Then the following question is: As the content of the information is increased, it is not rational to display all the information alone through a page. So, the solution is to adopt paging technology.

1,rs. RecordCount
Obviously, RecordCount is used to show how many records there are in a database table, or how many rows there are in a table. Often used in the pagination is a total of n articles, such as total information display.

2,rs. PageSize
Rs. PageSize is also the size of a page, which means that an ASP page can display the number of records. Values are defined by themselves, such as every page that is often seen displaying n articles.

3,rs. AbsolutePage and Rs.pagecount
When it comes to pagination, you must not mention Rs. AbsolutePage. The main function of the AbsolutePage property of a recordset is to decide what page is currently displayed. Its value is based on the designation of Rs. PageSize, then the Rs.pagecount information value is the Rs.recordcount and rs.pagesize divisible result. For example: General Information records Rs. RecordCount total 20, each page shows the number of Rs. PageSize set to 5, then the number of pages Rs.pagecount is 20/5=4 page times, and Rs. AbsolutePage is only the 1th page, page 2nd ... 4th page.

Speaking of now, get a specific procedure to debug. Continue to modify the showit.asp as follows:


<!--#include file= "conn.asp"-->

<%
Set rs = Server.CreateObject ("ADODB.") Recordset ")
sql = "SELECT * from Cnarticle ORDER BY cn_id Desc"
Rs. Open sql,conn,1,1
%>

<%
Page=request.querystring ("page") ' page value is an accepted value
Rs. PageSize = 2 ' show number of records per page
Rs. AbsolutePage = Page ' shows the number of pages that the current page is equal to receive
%>

<%
For i = 1 to Rs. PageSize ' use for Next loop to read the record of the current page sequentially
If Rs. EOF Then
Exit for
End If
Response.Write ("<br> article content is:" & RS ("Cn_content"))
Rs. MoveNext
Next%>

<%
Rs.close
Set rs = Nothing
Conn.close
Set conn=nothing
%>




Here, you debug the premise is that the database records relative to more than 4, so that the test effect is obvious, and the test method is to add showit.asp after the page=1 or page=2, such as debugging observation page display effect.

In fact, in the final analysis, the display of database content is


<%
For i = 1 to Rs. PageSize
If Rs. EOF Then
Exit for
End If
Response.Write ("<br> article content is:" & RS ("Cn_content"))
Rs. MoveNext
Next%>



, but imagine that the program should show only 2 messages (2 messages that have remained unchanged). But why add page=1 and page=2 will show different results? ...... That's definitely Rs. AbsolutePage the role of the. This is clear, I believe that the overall structure of pagination is a bit of a figure.



<!--#include file= "conn.asp"-->

<%
Set rs = Server.CreateObject ("ADODB.") Recordset ")
sql = "SELECT * FROM Cnarticle"
Rs. Open sql,conn,1,1
%>

<%filepath=request.servervariables ("Path_info")%>
<%
Page=request.querystring ("page") ' page value is an accepted value
Rs. PageSize = 2 ' show number of records per page
If not IsEmpty (page) Then ' if page has been initialized ...
If not isnumeric (page) Then ' determines whether the page value is a number
Page=1
Else
page = CInt (page) ' receives page and assigns the page variable to a numeric type
End If
If Page > Rs. PageCount Then ' if the number of pages received is greater than the total number of pages
Rs. AbsolutePage = Rs. PageCount ' Set the current display page equals the last page
ElseIf page <= 0 Then ' if page is less than or equal to 0
Rs. AbsolutePage = 1 ' Set current display page equals first page
Else
Rs. AbsolutePage = Page ' If greater than 0, display the current page equals the number of pages received
End If
Else
Rs. Absolutepage=1
End If
Page = Rs. Absolutepage%>

<%
For i = 1 to Rs. PageSize ' use for Next loop to read the record of the current page sequentially
If Rs. EOF Then
Exit for
End If
Response.Write ("article title is:" & RS ("Cn_title"))
Response.Write ("<br> article author is:" & RS ("Cn_author"))
Response.Write ("<br> article Add Time is:" & RS ("Cn_time"))
Response.Write ("<br> article content is:" & RS ("Cn_content"))
Response.Write ("Rs. MoveNext
Next
%>

<form action= "<%=filepath%>" method= "Get" >
<!--first to ensure that the total number of pages is not 1, not 0-->
<%if rs.pagecount<>1 and Rs.pagecount<>0 then%>
<!--If the current page is greater than 1, you should display the first and previous page connections-->
<%if page>1 then%>
[<a href= "&LT;%=FILEPATH%&GT;? page=<% = 1%> "> Home </a>]
[<a href= "&LT;%=FILEPATH%&GT;? page=<% = page-1%> "> Prev </a>]
<!--If the current page is greater than 1 and is less than the total number of pages, display the last and next page connections-->
<%if Page<rs.pagecount then%>
[<a href= "&LT;%=FILEPATH%&GT;? page=<% = page + 1%> "> next </a>]
[<a href= "&LT;%=FILEPATH%&GT;? page=<% = Rs. Pagecount%> "> Last </a>]
<!--If the current page is greater than 1 and is still greater than or equal to the total number of pages, do not display the last and next page connections-->
<%else%>
[Next Page] Last
<%end if%>
<!--Otherwise, the current number of pages is not greater than 1, only the end and next page of the connection-->
<%else%>
Home [Previous Page]
[<a href= "&LT;%=FILEPATH%&GT;? page=<% = page + 1%> "> next </a>]
[<a href= "&LT;%=FILEPATH%&GT;? page=<% = Rs. Pagecount%> "> Last </a>]
<%end if%>
<!--Eventually, if the total number of pages is 1, 0 there is no connection-->
<%else%&



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.