Using the recordset object paging method to show a limited # of records per webpage

 

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
September 01, 1999
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Displaying a certain # of records on each webpage Paging

Page 1-The page with the dropdown box.

<html><head>
<title>paging</title></head>
<body>
<form method="post" action="paging.asp" name="form1">
<select name="d1">
<option value="10">10</option>
<option value="7">7</option>
<option value="5">5</option>
</select>
<input type="submit" value="submit" name="b1">
</form>
</body>

Page 2-The page that shows the records

<%@ Language="VBScript"%>
<!-- #INCLUDE FILE="./adovbs.Inc" -->
<%
' I'm using a DSN-less connection.

ConnString = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("paging.mdb")

' Declare variables
Dim iPageSize        'How big our pages are
Dim strPageCount        'The number of pages we get back
Dim strPageCurrent    'The page we want to show
Dim strSql            'SQL select to limit fields
Dim conn    'The connection object
Dim rs        'The recordset object
Dim x                 'Standard looping var

If request("d1") <> "" Then Session("d1") = request("d1")

'Get parameters from the dropdown box on the previous page.
' You could easily just use the default of 10

iPageSize = Session("d1")
If Request("page") = "" Then
    strPageCurrent = 1
Else
    strPageCurrent = CInt(Request("page"))
End If

'Create db connection and recordset objects
'Open the connection string

Set conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
conn.Open ConnString

' Set cursor location and pagesize
rs.CursorLocation = adUseClient
rs.PageSize = iPageSize

'set sql statement to a local variable
strSql = "SELECT * FROM table1 ORDER BY id;"

' Open Recordset object
rs.Open strSql, conn, adOpenStatic, adLockReadOnly, adCmdText

' Get the count of the pages using the given page size
strPageCount = rs.PageCount

' If the request page falls outside the range,
' give them the closest match (1 or max)

If 1 > strPageCurrent Then strPageCurrent = 1
If strPageCurrent > strPageCount Then strPageCurrent = strPageCount

' Move to the selected page
rs.AbsolutePage = strPageCurrent

' Start output with a page x of n line
Response.Write "<FONT SIZE=""+1"">Page <B>"
Response.Write strPageCurrent
Response.Write "</B> of <B>"
Response.Write strPageCount
Response.Write "</B></FONT><BR><BR>" & vbCrLf

' Continue with a title row in our table
Response.Write "<TABLE BORDER=""1"">" & vbCrLf

' Show field names
Response.Write vbTab & "<TR>" & vbCrLf
For x = 1 To rs.Fields.Count
Response.Write vbTab & vbTab & "<TD><B>"
Response.Write rs.Fields(x - 1).Name
Response.Write "<B></TD>" & vbCrLf
Next
Response.Write vbTab & "</TR>" & vbCrLf

' Loop through our records
Do While rs.AbsolutePage = strPageCurrent And Not rs.EOF
    Response.Write vbTab & "<TR>" & vbCrLf
    For x = 1 To rs.Fields.Count
    Response.Write vbTab & vbTab & "<TD>"
    Response.Write rs.Fields(x - 1)
    Response.Write "</TD>" & vbCrLf
    Next
    Response.Write vbTab & "</TR>" & vbCrLf

'Move to the next record!
    rs.MoveNext
Loop

'Closing html table tag
Response.Write "</TABLE>" & vbCrLf

' Close all objects and clear from Memory
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing

'Show "previous" and "next"  links which navigate between pages
If strPageCurrent <> 1 Then
    Response.Write "<A HREF=""./paging.asp?page="
    Response.Write strPageCurrent - 1
    Response.Write """>Previous Page</A>" & vbCrLf
    'Spacer - inside the if so we don't get it unless needed
    Response.Write "&nbsp;&nbsp;" & vbCrLf
End If
If strPageCurrent < strPageCount Then
    Response.Write "<A HREF=""./paging.asp?page="
    Response.Write strPageCurrent + 1
    Response.Write """>Next Page</A>" & vbCrLf
End If

%>

<html><head>
<title>paging demo</title></head>
<body>
</body>

blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials