|
Paging of recordset data is often
required to display data in a descent manner. For example you can
show 10 rows of data per page, when there is a total of more than
100 pages.
Asp ado paging.
In this example we are connecting
to the ms access database named db1 and using sql we are quering the
table named mytable. Then we are displaying all the rows from the
tabale.
After querying the database we are
setting how many rows to set per page with
ors.PageSize = 2. Then
we are setting which particular page to display now with
ors.AbsolutePage = currentPage
. Here currentpage is the number of the page to be displayed.
Then at the bottom we are
displaying two links to browse to the next or previous page.
<%
Set oRs = Server.CreateObject("ADODB.Recordset")
Set db = Server.CreateObject("ADODB.connection")
ConStr = "DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;DBQ="
& server.MapPath(".") & "\db1.mdb"
db.Open ConStr
mySQL = "Select * from mytable;"
' mySQL = "Select * from mytable where name='Jhon';"
' mySQL = "Select * from mytable where address like
'%South%';"
oRs.Open mySQL , db, 1,2
' Set the page size of the recordset
ors.PageSize = 2
' Get the number of pages
nPageCount = ors.PageCount
currentPage=1
if request("Page")<>"" Then currentPage=int(request("Page"))
' Show page
ors.AbsolutePage = currentPage
response.write "Total pages: " & nPageCount & "<hr>"
response.write "Records per pages: " & ors.PageSize & "<hr>"
response.write "Current page: " & currentPage & " of " &
nPageCount & "<hr>"
For intRecord = 1 To ors.PageSize
response.write ors("id") & "<br>"
response.write ors("name") & "<br>"
response.write ors("email") & "<br>"
response.write ors("address") & "<br>"
response.write ors("tel") & "<br>"
response.write "<hr>"
Next
ors.close
db.close
Set db = nothing
Set oRs = Nothing
nextpage=currentPage+1
previouspage=currentPage-1
%>
<%if currentPage<>1 Then%>
<a href='testpaging.asp?page=<%=previouspage%>'>Prev
page</a>
<%end if
if currentPage<>nPageCount Then
%>
<a href='testpaging.asp?page=<%=nextpage%>'>Next page</a>
<%end if%> |
[Download
Sample Code]
|