ASP uses the properties and methods of the Recordset object to implement pagination

Source: Internet
Author: User
Tags implement query rowcount
Objects | paging

The basic method of paging display in ASP after understanding the properties and methods of the Recordset object, let's consider how we can use them to achieve the purpose of paging. First, we can set a value for the PageSize property to specify the number of rows that make up a page from the group of records, and then determine the total number of records by the RecordCount property, and then divide the total number of records by PageSize to get the total number of pages displayed. ; Finally, access to the specified page can be completed through the AbsolutePage property. As if it's not complicated, let's take a look at how the program should be implemented.

We build such a simple BBS application, its database has the following five fields: "ID", the automatic number of each post, "subject", the subject of each post, "name", the user's name, "email", the user's e-mail address; "Postdate "and add a post to the time." The DSN for the database is "BBS". We'll put all the steps for displaying the post pagination in a process called "showlist ()" for easy invocation. The procedure is as follows:

'----BBS display pagination----
< Sub showlist () >
<
Pgsz=20 ' Set switch, specify the number of posts displayed on each page, default to 20 posts a page
Set Conn = Server.CreateObject ("ADODB. Connection ")
Set RS = Server.CreateObject ("ADODB.") RecordSet ")
sql = "SELECT * from Message ORDER by ID DESC"
' Query all posts and follow the ID of the post in reverse order
Conn.Open "BBS"
Rs.Open sql,conn,1,1
If RS. Recordcount=0 Then
Response.Write "< p>< center> Sorry, there is no relevant information in the database! </center></p> "
Else
Rs. PageSize = Cint (PGSZ) ' Sets the value of the PageSize property
Total=int (RS.RECORDCOUNT/PGSZ *-1) *-1 ' calculates the total number of pages that can be displayed
Pageno=request ("PageNo")
If pageno= "" Then
PageNo = 1
Else
Pageno=pageno+1
Pageno=pageno-1
End If
Scrollaction = Request ("Scrollaction")
If scrollaction = "Previous page" Then
Pageno=pageno-1
End If
If scrollaction = "Next Page" Then
Pageno=pageno+1
End If
If PageNo < 1 Then
PageNo = 1
End If
N=1
Rs. AbsolutePage = PageNo
Response.Write "< center>"
Position=rs. Pagesize*pageno
Pagebegin=position-rs.pagesize+1
If position < RS. RecordCount Then
Pagend=position
Else
Pagend= RS. RecordCount
End If
Response.Write "< p>< font color= ' Navy ' >< b> database query results:</b>"
Response.Write "(total) &rs. RecordCount & "The information that conforms to the condition, displays" &pagebegin& "-" &pagend& ") </font></p>"
Response.Write "< TABLE width=600 border=1 cellpadding=4 cellspacing=0 bgcolor= #FFFFFF >"
Response.Write "< TR bgcolor= #5FB5E2 >< FONT size=2 >< TD >< B > Theme </></td >< T D >< B > user </></td >< TD >< B >Email</></td >< TD >< B > Cloth Date </b ></td ></font >< TR bgcolor= #FFFFFF > "
Do While not (RS are nothing)
RowCount = RS. PageSize
Do but not RS. EOF and RowCount > 0
If N=1 Then
Response.Write "< TR bgcolor= #FFFFFF >"
ELSE
Response.Write "< TR bgcolor= #EEEEEE >"
End If
N=1-n >
< td>< span style= "font-size:9pt" >< A href= ' view.asp?key=< =rs ("ID") > ' >< =rs ("subject") ></a></span></td>
< td>< span style= "font-size:9pt" >< =rs ("name") ></a></span></td>
< td>< span style= "font-size:9pt" >< a href= "mailto:< =rs (" email ") >" >< =rs ("email") >< /a></SPAN> </td>
< td>< span style= "font-size:9pt" >< =rs ("postdate") ></span> </td>
</tr>
<
RowCount = RowCount-1
Rs. MoveNext
Loop
Set rs = rs. NextRecordset
Loop
Conn.close
Set rs = Nothing
Set Conn = Nothing
%>
</table >
< FORM method=get action= "list.asp" >
< INPUT type= "HIDDEN" name= "PageNo" value= "< =pageno >" >
<
If PageNo > 1 Then
Response.Write "< INPUT type=submit name= ' scrollaction ' value= ' prev ' >"
End If
If rowcount = 0 and PageNo < >total Then
Response.Write "< INPUT type=submit name= ' scrollaction ' value= ' next page ' >"
End If
Response.Write "</form>"
End If
>
<% End Sub%>



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.