ASP Paging

Related Articles

Step1

Create a simple database in MS Access. Name the database as Sample.mdb

Step2

Create A table called Employee. Add fields such as ID, Name, Address. Set the ID as Autonumber and enter 20 records into the table.

Step 3

Open Notepad and type the following given code. Save the file as ASP_Paging.asp

<@ language=vbscript @>
<!--#include file=paging_inc.asp-->
<%
dim Rst
set Rst=server.CreateObject"ADODB.recordset")
Rst.Open "select * from employee",Connect,3,1,1
if Rst.EOF =true then
     Response.Write "No  Records Avialable in the Database" 
else
    dim intPageSize,PageIndex,TotalPages 
    dim RecordCount,RecordNumber,Count intpageSize=5
PageIndex=request("PageIndex")
 if PageIndex="" then
      PageIndex=1 RecordCount=Rst.RecordCount  
RecordNumber=(intPageSize * PageIndex) - intPageSize
Rst.PageSize =intPageSize
Rst.AbsolutePage = PageIndex TotalPages=Rst.PageCount
dim intPrev,intNext intPrev=PageIndex - 1 intNext=PageIndex +1
Count=1
Response.Write "<table align=center>"
response.write "<tr bgcolor=yellow><th>ID</th><th>Name</th><th>Address</th></tr>"
while not Rst.eof and Count<=intPageSize
     response.write "<tr><td>" & Rst.fields(0) & "</td><td>" & Rst.fields(1) & "</td><td>" & Rst.fields(2) & "</td></tr>"
Rst.movenext
Count=Count +1
wend
response.write "</table>"
call button(intPrev,intNext,TotalPages)
set Rst=nothing
Connect.close
end if
%>

Now open a empty notepad again save it as Paging_inc.asp Type the code given below:

<%
Sub button(intPrev,intNext,TotalPages)
dim Counter
if intPrev <> 0 then
     response.write "<a href=ASP_Paging.asp?PageIndex=" & intPrev & ">Previous Page</a>"
end if
if Rst.AbsolutePage <> -3 then
   response.write "<a href=ASP_PAging.asp?PageIndex=" & intNext  & ">Next Page </a>"
end if
Counter=1
response.write "<br>Navigate to Page :"
While Counter <=TotalPages
   if cint(Counter)=cint(PageIndex) then
      response.write Counter
   else
      response.write "<a href=ASP_Paging.asp?PageIndex=" & Counter & ">" & Counter & "</a>"
   end if

if cint(Counter) <> cint(TotalPages) then
    response.write " | "
End if
Counter= Counter +1
wend
End Sub

Function Connect()
set Connect=server.createobject("ADODB.connection")
Connect.open "Provider=microsoft.jet.oledb.4.0;Data source=" & server.mappath("Sample.mdb")
End Function
%>

Do not forget to place all the three files

  1. ASP_PAging.asp
  2. Paging_inc.asp
  3. Sample.mdb

inside the same folder.

Now Run the ASP_Paging.asp Page in Internet Explorer. You willl see data getting reflected from the database.

Since the intPageSize variable set to 5. A page will display 5 rows of data. To See other data you need to navigate to Other Pages by clicking the Previous Page, Next Page and Page Number.

Hope this article would help a web programmer in implementing Paging Techniques in ASP.

For more clarifications you can download the free code from http://webdeveloperindia.com/asp_Paging_Technique.asp

For more information write to us @ info@webdeveloperindia.com


Publication Date: Sunday 11th July, 2004
Author: U.swaroop View profile

Related Articles