|
' 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 !
|