The paging program for ADO components

Source: Internet
Author: User
Tags add time include modify prev query servervariables split
It should be said, learned how to insert records, how to display records, then the 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")
Rs. PageSize = 2 ' show number of records per page

%>


For i = 1 to Rs. PageSize ' use for Next loop to read the record of the current page sequentially
If Rs. EOF Then


Response.Write ("<br> article content is:" & RS ("Cn_content"))
Rs. MoveNext
Next%>



Set rs = 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


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") The page value is the accepted value
Rs. PageSize = 2 ' show number of records per page
if not isempty (page) Then ' If page is already initialized ...
If not isnumeric (page) Then ' judge whether the page value is a number
page=1
Else
page = CInt (page) ' receives a page and assigns it to a numeric type Pag The e variable
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 ' Sets the current display page equals the last page
ElseIf page <= 0 Then ' if page is less than or equal to 0
Rs. AbsolutePage = 1 ' Setting the current display page is equal to the first page
Else
Rs. AbsolutePage = Page ' If greater than 0, displays the number of pages that the current page is equal to receive
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%>
Home [Previous Page] [Next Page] Last
<%end if%>

[Page time: <font color=red><b><%=page%></b></font>/<%=rs. Pagecount%>]
[Altogether <%=rs. Recordcount%> article <font color=red><b><%=rs. Pagesize%></b></font> article/page]
Go to <input name= "page" size=5 value= "<%=page%>" > page
<input type= "Submit" value= "Enter" >
</form>

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

Long pagination code, to understand is really not easy, the platform to refer to the need to modify is also more trouble. Finally, a function can be made, the next call will be very convenient.


<%
function pagination (pagecount,pagesize,page,resultcount)
Dim query, a, X, temp
Action = "http://" & Request.ServerVariables ("Http_host") _
& Request.ServerVariables ("Script_name")
query = Split (Request.ServerVariables ("query_string"), "&")
For each x in query
A = Split (x, "=")
If StrComp (A (0), "page", vbTextCompare) <> 0 Then
Temp = temp & A (0) & "=" & A (1) & "&"
End If
Next
Response.Write ("<form method=get onsubmit=" "Document.location =" "_
& Action & "?" & Temp & "Page= ' +this.page.value;return false;" > ")
If Page<=1 Then
Response.Write ("[Home] [prev]")
Else
Response.Write ("[<a href= & Action &"? "& Temp &" Page=1>_
Home </a>] "
Response.Write ("[<a href=" & Action & "?" & Temp & "Page=" _
& (Page-1) & "> previous page </a>]")
End If

If Page>=pagecount Then
Response.Write ("[Next page] [last]")
Else
Response.Write ("[<a href=" & Action & "?" & Temp & "Page=" _
& (page+1) & "> Next page </a>]")
Response.Write ("[<a href=" & Action & "?" & Temp & "Page=" _
& PageCount & "> Last </a>]")
End If
Response.Write ("[Page: <font color=red>" & Page & "</font>/" & PageCount)
Response.Write ("] [Total" & Resultcount & "<font color=red>" & Pagesize_
& "</font> bar/page]")
Response.Write ("Go" & "<input name=page size=4 value=" & Page & ">" _
& "page <input type=submit value=go>")
End Function
%>

To reference, you can:


<%call Pagination (Rs. Pagecount,rs.pagesize,page,rs. RecordCount)%>




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.