Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ADO : Using ASP pages to page through Recordsets
 
' Now showing first, next, back, last buttons.

Response.Write "<div align=""center"">" & vbcrlf
Response.Write "<a href="""
Response.Write Request.ServerVariables("SCRIPT_NAME")
Response.Write "?pagenum=1""><b>First Page</b></a>"
Response.Write "	|	"

If abspage = 1 Then
	Response.Write "<span style=""color:silver;"">Previous Page</span>"
Else
	Response.Write "<a href=""" & Request.ServerVariables("SCRIPT_NAME")
	Response.Write "?pagenum=" & abspage - 1 & """><b>Previous Page</b></a>"
End If

Response.Write "	|	"
	If abspage < pagecnt Then
		Response.Write "<a href=""" & _
			Request.ServerVariables("SCRIPT_NAME")
		Response.Write "?pagenum=" & abspage + 1 & """>Next Page</a>"
	Else
		Response.Write "<span style=""color:silver;""" & _
			"><b>Next Page</b></span>"
End If

Response.Write "	|	"
Response.Write "<a href=""" & Request.ServerVariables("SCRIPT_NAME")
	Response.Write "?pagenum=" & pagecnt & """><b>Last Page</b></a>"
Response.Write "</div>" & vbcrlf

Else
	Response.Write "No records found!"
End If

rs.Close
Set rs = Nothing

%>
</body>
</html>

Explanation
Above code creates the navigation links under the records shown to the user letting him move through the records the way he wants.

	Response.Write "<div align=""center"">" & vbcrlf
	Response.Write "<a href="""
	Response.Write Request.ServerVariables("SCRIPT_NAME")
	Response.Write "?pagenum=1""><b>First Page</b></a>"
	Response.Write "	|	"

This part of the code creates a link to the 'First Page' of the recordset. Here we create a link to our paging.asp page itself by using the Request.ServerVariables("SCRIPT_NAME") variable. Then we set the pagenum variable in the querystring to 1 thus setting the AbsolutePage property to 1 and ending up showing the first page of the recordset.

	If abspage = 1 Then
		Response.Write "<span style=""color:silver;""" & _
			">Previous Page</span>"
	Else
		Response.Write "<a href=""" & _
			Request.ServerVariables("SCRIPT_NAME")
		Response.Write "?pagenum=" & abspage - 1 & """" & _
			"><b>Previous Page</b></a>"
	End If

Now we create 'Previous Page' link which will move the user back to the previous page in the recordset.

	Response.Write "	|	"
	If abspage < pagecnt Then
		Response.Write "<a href=""" & _
			Request.ServerVariables("SCRIPT_NAME")
		Response.Write "?pagenum=" & abspage + 1 & """>Next Page</a>"
	Else
		Response.Write "<span style=""color:silver;""" & _
			"><b>Next Page</b></span>"
	End If

Here we use a bit of logic to create the 'Next Page' link. We check to see if AbsolutePage is set to less than PageCount then show the link otherwise show the 'Next Page' text but don't provide a link.

	Response.Write "	|	"
	Response.Write "<a href=""" & _
		Request.ServerVariables("SCRIPT_NAME")
		Response.Write "?pagenum=" & pagecnt & """><b>" & _
			"Last Page</b></a>"
	Response.Write "</div>" & vbcrlf

In the end we create the 'Last Page' link. We do that by setting the pagenum variable to PageCount property.

Tips
Paging through the recordset is very simple. The only thing different from simply accessing the database and showing it's records to showing the records in a page by page fashion is to set CursorLocation to 'adUseClient' and PageSize to the number of records you want to show per page. After that open the database as you would do normally.

Then set AbsolutePage to the recordset page you want to show to the user. You can set it manually or set it by using a bit of logic as we did in the paging.asp page built in this article.

Now you are all set. Simply use a For...Next loop as discussed in the article and show the records.

Now tell me it was difficult. If you are unsure read it over because this is some thing which you will use repeatedly in your ASP database pages and makes seeing and moving through records more easy and fun.

Summary
We have learned how to show records in a page by page fashion using ASP pages. We saw that it was very easy and all we had to do was to set three Recordset properties and that's it. Enjoy !

' Now showing first, next, back, last buttons.

Response.Write "<div align=""center"">" & vbcrlf
Response.Write "<a href="""
Response.Write Request.ServerVariables("SCRIPT_NAME")
Response.Write "?pagenum=1""><b>First Page</b></a>"
Response.Write "	|	"

If abspage = 1 Then
	Response.Write "<span style=""color:silver;"">Previous Page</span>"
Else
	Response.Write "<a href=""" & Request.ServerVariables("SCRIPT_NAME")
	Response.Write "?pagenum=" & abspage - 1 & """><b>Previous Page</b></a>"
End If

Response.Write "	|	"
	If abspage < pagecnt Then
		Response.Write "<a href=""" & _
			Request.ServerVariables("SCRIPT_NAME")
		Response.Write "?pagenum=" & abspage + 1 & """>Next Page</a>"
	Else
		Response.Write "<span style=""color:silver;""" & _
			"><b>Next Page</b></span>"
End If

Response.Write "	|	"
Response.Write "<a href=""" & Request.ServerVariables("SCRIPT_NAME")
	Response.Write "?pagenum=" & pagecnt & """><b>Last Page</b></a>"
Response.Write "</div>" & vbcrlf

Else
	Response.Write "No records found!"
End If

rs.Close
Set rs = Nothing

%>
</body>
</html>

Explanation
Above code creates the navigation links under the records shown to the user letting him move through the records the way he wants.

	Response.Write "<div align=""center"">" & vbcrlf
	Response.Write "<a href="""
	Response.Write Request.ServerVariables("SCRIPT_NAME")
	Response.Write "?pagenum=1""><b>First Page</b></a>"
	Response.Write "	|	"

This part of the code creates a link to the 'First Page' of the recordset. Here we create a link to our paging.asp page itself by using the Request.ServerVariables("SCRIPT_NAME") variable. Then we set the pagenum variable in the querystring to 1 thus setting the AbsolutePage property to 1 and ending up showing the first page of the recordset.

	If abspage = 1 Then
		Response.Write "<span style=""color:silver;""" & _
			">Previous Page</span>"
	Else
		Response.Write "<a href=""" & _
			Request.ServerVariables("SCRIPT_NAME")
		Response.Write "?pagenum=" & abspage - 1 & """" & _
			"><b>Previous Page</b></a>"
	End If

Now we create 'Previous Page' link which will move the user back to the previous page in the recordset.

	Response.Write "	|	"
	If abspage < pagecnt Then
		Response.Write "<a href=""" & _
			Request.ServerVariables("SCRIPT_NAME")
		Response.Write "?pagenum=" & abspage + 1 & """>Next Page</a>"
	Else
		Response.Write "<span style=""color:silver;""" & _
			"><b>Next Page</b></span>"
	End If

Here we use a bit of logic to create the 'Next Page' link. We check to see if AbsolutePage is set to less than PageCount then show the link otherwise show the 'Next Page' text but don't provide a link.

	Response.Write "	|	"
	Response.Write "<a href=""" & _
		Request.ServerVariables("SCRIPT_NAME")
		Response.Write "?pagenum=" & pagecnt & """><b>" & _
			"Last Page</b></a>"
	Response.Write "</div>" & vbcrlf

In the end we create the 'Last Page' link. We do that by setting the pagenum variable to PageCount property.

Tips
Paging through the recordset is very simple. The only thing different from simply accessing the database and showing it's records to showing the records in a page by page fashion is to set CursorLocation to 'adUseClient' and PageSize to the number of records you want to show per page. After that open the database as you would do normally.

Then set AbsolutePage to the recordset page you want to show to the user. You can set it manually or set it by using a bit of logic as we did in the paging.asp page built in this article.

Now you are all set. Simply use a For...Next loop as discussed in the article and show the records.

Now tell me it was difficult. If you are unsure read it over because this is some thing which you will use repeatedly in your ASP database pages and makes seeing and moving through records more easy and fun.

Summary
We have learned how to show records in a page by page fashion using ASP pages. We saw that it was very easy and all we had to do was to set three Recordset properties and that's it. Enjoy !


Previous ( 1 Gone )( No Further Pages )

See all comments and questions (post-ad) posted for this tutorial.


Download Associated Files
2000071001.zip

Related Articles
  1. How to display records from top 4 database systems using plain ASP?
  2. Uploading Files into an Access Database using plain ASP
  3. Displaying Images from an Access Database using plain ASP
  4. Fastest way of Database Access : Caching Records in Memory
  5. Adding records to the database with ASP
  6. DSN vs DSN less Database Connections
  7. Searching and sorting records in a recordset from the database
  8. Speedup Database Access using GetRows
  9. Inserting Form content into Database with ASP
  10. Generating Random Records from the Database

Comments/Questions ( Threads: 22, Comments: 24 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Paging through record sets
  2. Paging through recordsets
  3. Showing only the first record
  4. Help pls....
  5. Using ASP pages to page through Recordsets
  6. Great Article
  7. HELP! Using the paging ASP code for inter join record sets?
  8. Error ADODB.Recordset error '800a0bcd'
  9. asp
  10. Having problem when inserting a sql statement
  11. Using SELECT in your example
  12. urgent please
  13. Just Wondering
  14. Watch the provider
  15. How to show table records, based on another query results? ( 1 Reply )
  16. what a guy hay
  17. Combining 2 of your ASP codes
  18. using a query
  19. Help please paging
  20. Drill Down
  21. Displaying records in a table format
  22. Shorting? ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.