Introduction to ASP Programming (20): ADO component of the paging Program _asp Foundation

Source: Internet
Author: User
Tags add time prev servervariables
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 pagination is there are n articles altogetherAnd so on, the total information is displayed.

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 those that are often seen Show n posts per pageInformation like that.

3,rs. AbsolutePage and Rs.pagecount
When it comes to pagination, you must not mention Rs. AbsolutePage. The main function of the AbsolutePage attribute of a recordset is to determine The first few pages are 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 showit.aspThe following modifications are made:


<!--#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 the database records relative to more than 4, so the test effect is obvious; and the test method is showit.asp back .Add to ? page=1Or ? page=2And so on 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%>


The role, but imagine: the program should becan only display 2 messages (2 messages that have remained unchanged). But why add ? page=1And ? page=2will 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%>
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
%>



Effects page See:
http://www.cnbruce.com/database/

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) &" > Prev </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>/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)%>
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.