ASP: Digg Style Pagination Script

Written by JOJY, on 03.30.2007

This is ASP based pagination script, similiar to Digg.com pagination. The credit goes to Jason Coleman who written this script in PHP and I simply converted this into ASP. I have attached screenshot this will help you to see how it looks like.

Demo Digg Style Pagination Script

Download Script

Usage

Many people were asking how to add this code into our own projects. I have included the code, I hope this will help you in integrating this script.

<!–#include file=”pagination.asp” –>
<%
dim objcon, objrs, totalpages, page, numPerPage, countItem
set objcon=Server.CreateObject(”ADODB.Connection”)
set objrs=Server.CreateObject(”ADODB.RecordSet”)
countItem = 0
numPerPage = 5
objrs.cursorlocation = 3
if request(”page”) = “” then
page=1
else
page=cLng(request(”page”))
end if
objcon.open connectionString
objrs.open “SELECT * FROM Customers”, objcon,1,2
if objrs.EOF then
Response.Write(”<h2>Sorry! No Search Result Found</h2>”)
else
objRs.pageSize = numPerPage
totalpages = objRs.PageCount
objRs.absolutePage=page
while not objrs.EOF AND countItem < objRs.pageSize
Response.Write(”YOUR CODE GOES HERE”)
countItem = countItem + 1
objrs.movenext()
wend
ps = getPaginationString(page, objrs.RecordCount, numPerPage, 2, “demo.asp?yourcustomparams=PARAM&name=NAME&Search=Search”)
Response.Write(ps)
end if
‘always cleanup things
objrs.close()
objcon.close()
set objrs=nothing
set objcon=nothing
%>

  • Print
  • Send to friend
  • Save this to del.icio.us
fake ad - add real google adsense code in featuredad.php

Leave a Comments »

Trackback / RSS 2.0

1. Michael - April 9, 2007

What have you found to be the best way to convert php scripts to asp/asp.net?

2. JOJY - April 9, 2007

you should have knowledge of asp/php syntax.. functions are almost similar or you may find easily by searching documentation.. I have good knowledge in both languages.. so it took just 25 mins to convert this script.

3. Stupidcat - May 10, 2007

Can you provide an example of using this script? Thank you.

4. JOJY - May 10, 2007

Its simple.. download the script, run the file pagination.asp .. and you will the demo!

5. Tempa - May 26, 2007

Hi,

There is a bug in the script. When you increase the adjacent value to more then 2 to let say 4 it disables the “Next” button when you reach the last page just before the first break “…”.

Regards,

6. Tempa - May 26, 2007

Near Line 115:
[CODE]
‘close to beginning only hide later pages
if page

7. JOJY - May 26, 2007

Tampa, when you reach to last page it will obviously disable the next button.. there isn’t any bug..

8. Lucas Soler - July 12, 2007

Hello !!
Excelent Script, many thanks !!!!
Only one bug, try this :

Press “Next” Button, and then press the last page. The script go to page 1, and no the last. This problem is becouse the parametrers is ?foo=barpage=938 (and the correct syntax is ?foo=bar&page=938). The “&” betwen bar and page).

Thanks again, and sorry for my bad, bad english

9. Lucas Soler - July 12, 2007

Hello again, I found the error in line 147

pagination = pagination & ““&lastpage&”

The correct is

pagination = pagination & ““&lastpage&”

10. Alejandro Barrere - October 27, 2007

Hi,

Good script but question… how do you attached this into records from your database? (access)

I will appreciate your help.

Regards,
Alejandro.

11. Meg - November 29, 2007

Hi thanks alot fro the script. It works great. but i want to know how can i link it to a recordset. and how if i place the function in a new page and call it as a include file. right now i manage to call it from include file and i can display teh records..let say the 15 records. but when i click next it doesnt move to the next 15 records. please help..i really appreciate it.

sSQL1 = “SELECT * FROM country1 ”
set RsCountry = conn.Execute(sSQL1)
slimit=15
stargetpage=”testpagination.asp?foo=bar”
sadjacent=2
ps = getPaginationString(page, sstring, slimit, sadjacent, stargetpage)

If Not RsCountry.EOF Then

Country ID
CountryCode
CountryName
TelephoneCCode

<tr bgcolor=”" align=”center”>

this is in testpagination paeg and the function is in function page. right now the output of the code is a single record and stays the same on click of any page.

12. Jovanky De Los Santos - December 28, 2007

Meg and others, JoyJ is rigth, if you don’t dominate ASP you may have a problem applying this script to it. But this is as simple as it gets. All you have to do is look at the following piece of code:
ps = getPaginationString(page, 1500, 15, 2, “mytestpage3.asp?foo=bar”)

See, the number 1500 reflects the full amount of records your recordset will return, 15 reflects how many you want to display per page. So all you need to do is store the amount of returnede records into a variable and replace the 1500 with it, then establish the amount of records you want to be displayed per page by replacing number 15 with whatever number of records you want to show on each page. The last thing you will need to do is modify your SQL query to return the total amount of records so you can store it on the variable, but also based on the ?foo=bar&page=PageNumber value, you can use this as a querystring to tell your query the range of records you want to read from, for instance, if you determine that you have a total of 100 recs and want to split them by ten each, then your query shoud return 1 - 10 when ?foo=bar&page=1, then 10 - 20 when ?foo=bar&page=2 and so on…

Good luck

13. Jovanky De Los Santos - December 28, 2007

Here is a piece of the code I implemented, I hope it helps

Comm.CommandText = “Select Count(*) as Count from articles”
Set RS = Comm.Execute
Reqs = RS(”Count”)
Page = Request(”Page”)
If Page = “” then Page = 1
comm.commandtext = “SELECT *”&_
“FROM (SELECT ROW_NUMBER() OVER(ORDER BY Article_id) AS “&_
“rownum, Article_ID, Title, Body, Create_date, “&_
“meta_description FROM articles) AS Articles2 “&_
“WHERE rownum >= “&Page&” AND rownum <= “&Cint(page)+ 5

14. kerem yolak - January 22, 2008

This pagination looks great, I want to use it on my pages which I created with an asp code generator program(asprunner).I couldnt figure out where to paste the codes of this script…This is adress I want to use pagination script.

http://www.sohodvdcenter.com/output/Filmler_list.asp?orderby=dFilmSiparisNo&a=search&value=1&SearchOption=Contains&SearchField=FilmTuru&SearchFor=

Please help
Thanks
kerem

15. Sushant - March 26, 2008

Please put a full functional asp sample code..instead of putting in parts like Mr. Jovanky De Los Santos …for newbies
thanks in advance

16. monie - August 4, 2008

Hey mate, I really liked what you do with the script and I am dying to make it work on my ASP-MySQL page. I did manage to make it work on my PHP-MySQL page but not in my ASP page. Could you help me by giving an example on the working script together with the real data from the database? Thanks in advance!

17. Monie - August 7, 2008

Hey mate!
Can we have a more detail explaination that uses a real database in the example.
I am having a nightmare using this code :)
Thanks..

18. Monie - August 19, 2008

Ahh… thanks man for the great example! Well perhaps you could do the same tutorian with JQuery? That would be lovely!
Thanks…

19. Goodkat - November 10, 2008

Hi! Tell me please, what does function parameters means?
“getPaginationString(page, totalitems, limit, adjacents, targetpage)”

page - current page
totalitems - whole amount of pages in Recordset
limit - ?
adjacents - ?
targetpage - link to page.

20. reengineering - April 15, 2009

below is the code where i want to past the pagination. can u help me put your best pagination script in there? thank you so much!

<%

sFileName = “Default.asp”
sTemplateFileName = “Default.htm”

LoadTemplate sAppPath & sTemplateFileName, “main”

LoadTemplate sHeaderFileName, “Header”

SetVar “FileName”, sFileName

Header_Show
Grid_Show
Search_Show
EmpMonth_Show
Parse “Header”, False
Parse “main”, False
Response.write PrintVar(”main”)

‘————————————————–

Sub Grid_Show()

Dim sWhere
sWhere = “”
sOrder = “”
sSQL = “”
HasParam = false

SetVar “TransitParams”, “name=” & ToURL(GetParam(”name”)) & “&dep_id=” & ToURL(GetParam(”dep_id”)) & “&email=” & ToURL(GetParam(”email”)) & “&”
SetVar “FormParams”, “name=” & ToURL(GetParam(”name”)) & “&dep_id=” & ToURL(GetParam(”dep_id”)) & “&email=” & ToURL(GetParam(”email”)) & “&”
‘ Build WHERE statement

pdep_id = GetParam(”dep_id”)
if IsNumeric(pdep_id) and not isEmpty(pdep_id) then pdep_id = CLng(pdep_id) else pdep_id = Empty
if not isEmpty(pdep_id) then
HasParam = true
sWhere = sWhere & “e.dep_id=” & pdep_id
end if

pemail = GetParam(”email”)
if not isEmpty(pemail) then
if not (sWhere = “”) then sWhere = sWhere & ” and ”
HasParam = true
sWhere = sWhere & “e.email like ‘%” & replace(pemail, “‘”, “””) & “%’”
end if

pname = GetParam(”name”)
if not isEmpty(pname) then
if not (sWhere = “”) then sWhere = sWhere & ” and ”
HasParam = true
sWhere = sWhere & “e.name like ‘%” & replace(pname, “‘”, “””) & “%’”
end if

if HasParam then sWhere = ” AND (” & sWhere & “)”
‘ Build ORDER statement
sOrder = ” order by e.name Asc”
iSort = GetParam(”FormGrid_Sorting”)
iSorted = GetParam(”FormGrid_Sorted”)
sDirection = “”
if IsEmpty(iSort) then
SetVar “Form_Sorting”, “”
else
if iSort = iSorted then
SetVar “Form_Sorting”, “”
sDirection = ” DESC”
sSortParams = “FormGrid_Sorting=” & iSort & “&FormGrid_Sorted=” & iSort & “&”
else
SetVar “Form_Sorting”, iSort
sDirection = ” ASC”
sSortParams = “FormGrid_Sorting=” & iSort & “&FormGrid_Sorted=” & “&”
end if

if iSort = 1 then sOrder = ” order by e.name” & sDirection
if iSort = 2 then sOrder = ” order by e.title” & sDirection
if iSort = 3 then sOrder = ” order by d.name” & sDirection
if iSort = 4 then sOrder = ” order by e.work_phone” & sDirection
if iSort = 5 then sOrder = ” order by e.email” & sDirection
if iSort = 6 then sOrder = ” order by e.sponsor” & sDirection ‘added
if iSort = 7 then sOrder = ” order by e.joining_date” & sDirection ‘added
if iSort = 8 then sOrder = ” order by e.ip” & sDirection ‘added
end if

‘ Build full SQL statement

sSQL = “select e.dep_id as e_dep_id, ” & _
“e.email as e_email, ” & _
“e.emp_id as e_emp_id, ” & _
“e.name as e_name, ” & _
“e.title as e_title, ” & _
“e.work_phone as e_work_phone, ” & _

“e.joining_date as e_joining_date, ” & _
“e.ip as e_ip, ” & _
“e.sponsor as e_sponsor, ” & _
“d.dep_id as d_dep_id, ” & _
“d.name as d_name ” & _

” from emps e, deps d” & _
” where d.dep_id=e.dep_id ”

sSQL = sSQL & sWhere & sOrder
SetVar “SortParams”, sSortParams

‘ Open recordset
openrs rs, sSQL
if rs.eof then
‘ Recordset is empty
set rs = nothing
SetVar “DListGrid”, “”
Parse “GridNoRecords”, False
Parse “FormGrid”, False
exit sub
end if

‘added pagination
‘ Select current page
iPage = GetParam(”Formemps_Page”)
if IsEmpty(iPage) then iPage = 1
‘ Open recordset
openrs rs, sSQL
if rs.eof then
‘ Recordset is empty
set rs = nothing
SetVar “DListemps”, “”
Parse “empsNoRecords”, False
SetVar “empsScroller”, “”
Parse “Formemps”, False
exit sub
end if

aemp_level = Split(”0;;3;Admin”, “;”)
amanmonth = Split(”0;;1;Yes”, “;”)
iCounter = 0
while not rs.eof and iCounter < (iPage-1)*15
rs.movenext
iCounter = iCounter + 1
wend
iCounter = 0

‘end pagination

‘ Show main table based on recordset
while not rs.EOF and iCounter < 15
fldname = GetValue(rs, “e_name”)
fldtitle = GetValue(rs, “e_title”)
flddep_id = GetValue(rs, “d_name”)
fldwork_phone = GetValue(rs, “e_work_phone”)
fldemail = GetValue(rs, “e_email”)
fldemail=”” & fldemail & “
fldsponsor = GetValue(rs,”e_sponsor”)
fldjoining_date = GetValue(rs,”e_joining_date”)
fldip = GetValue(rs,”e_ip”)

SetVar “name”, ToHTML(fldname)
SetVar “name_URLLink”, “EmpDetail.asp”
SetVar “Prm_emp_id”, ToURL(GetValue(rs, “e_emp_id”))
SetVar “title”, ToHTML(fldtitle)
SetVar “dep_id”, ToHTML(flddep_id)
SetVar “work_phone”, ToHTML(fldwork_phone)
SetVar “email”, fldemail
SetVar “sponsor”, ToHTML(fldsponsor)
SetVar “joining_date”, ToHTML(fldjoining_date)
SetVar “ip”, ToHTML(fldip)

Parse “DListGrid”, True

rs.MoveNext
‘added
iCounter = iCounter + 1
‘end added
wend

‘added

‘ Parse scroller
if rs.EOF and iPage = 1 then
SetVar “empsScroller”, “”
else
if rs.EOF then
SetVar “empsScrollerNextSwitch”, “_”
else
SetVar “NextPage”, (iPage + 1)
SetVar “empsScrollerNextSwitch”, “”
end if

if iPage = 1 then
SetVar “empsScrollerPrevSwitch”, “_”
else
SetVar “PrevPage”, (iPage - 1)
SetVar “empsScrollerPrevSwitch”, “”
end if

SetVar “empsCurrentPage”, iPage
Parse “empsScroller”, False
end if

‘end added

set rs = nothing
SetVar “GridNoRecords”, “”
Parse “FormGrid”, False

End Sub

Sub Search_Show()

SetVar “ActionPage”, “Default.asp”

‘ Set variables with search parameters

flddep_id = GetParam(”dep_id”)
fldname = GetParam(”name”)
fldemail = GetParam(”email”)
‘ Show fields

SetVar “LBdep_id”, “”
SetVar “ID”, “”
SetVar “Value”, “All”
Parse “LBdep_id”, True
openrs rsdep_id, “select dep_id, name from deps order by 2″
while not rsdep_id.EOF
SetVar “ID”, GetValue(rsdep_id, 0) : SetVar “Value”, GetValue(rsdep_id, 1)
if cstr(GetValue(rsdep_id, 0)) = cstr(flddep_id) then SetVar “Selected”, “SELECTED” else SetVar “Selected”, “”
Parse “LBdep_id”, True
rsdep_id.MoveNext
wend
set rsdep_id = nothing

SetVar “name”, ToHTML(fldname)
SetVar “email”, ToHTML(fldemail)
Parse “FormSearch”, False
End Sub

Sub EmpMonth_Show()

Dim sWhere
sWhere = “”
sOrder = “”
sSQL = “”
HasParam = false

SetVar “TransitParams”, “”
SetVar “FormParams”, “”
‘ Build WHERE statement

sWhere = ” WHERE manmonth=1″

‘ Build full SQL statement

sSQL = “select e.name as e_name, ” & _
“e.picture as e_picture ” & _
” from emps e ”

sSQL = sSQL & sWhere & sOrder
‘ Open recordset
openrs rs, sSQL
if rs.eof then
‘ Recordset is empty
set rs = nothing
SetVar “DListEmpMonth”, “”
Parse “EmpMonthNoRecords”, False
Parse “FormEmpMonth”, False
exit sub
end if

‘ Show main table based on recordset
while not rs.EOF
fldpicture = GetValue(rs, “e_picture”)
fldname = GetValue(rs, “e_name”)
flddep_id = GetValue (rs, “d_name”)
fldpicture=”"

SetVar “picture”, fldpicture
SetVar “name”, ToHTML(fldname)
SetVar “d_name”, ToHTML(fldd_name)
Parse “DListEmpMonth”, True

rs.MoveNext

wend

set rs = nothing
SetVar “EmpMonthNoRecords”, “”
Parse “FormEmpMonth”, False

End Sub

%>

21. JOJY - April 15, 2009

I have already given the sample code but here is it again.

create connection and recordset obj
set objcon=Server.CreateObject(”ADODB.Connection”)
set objrs=Server.CreateObject(”ADODB.RecordSet”)
objcon.open “….”
countItem = 0
numPerPage = 5
objrs.cursorlocation = 3
if request(”page”) = “” then
page=1
else
page=cLng(request(”page”))
end if
objrs.open “SELECT * FROM SOMETABLE”, objcon,1,2
if not objrs.EOF then
objRs.pageSize = numPerPage
totalpages = objRs.PageCount
objRs.absolutePage=page
while not objrs.EOF AND countItem < objRs.pageSize
….. do some stuff here
countItem = countItem + 1
objrs.movenext()
wend
ps = getPaginationString(page, objrs.RecordCount, numPerPage, 2, “mypage.asp?optional=”&parameter&”&goes=”&here&”&Search=Search”)
Response.Write(ps)

end if
… clean things.. conn, record set

22. Bigxxt - April 29, 2009

Hi there people, I have just implemented the code, all seem ok apart from the fact I can show subsequent pages. My statement reads
ps = getPaginationString(page, objrs.RecordCount, numPerPage, 2,”product.asp? foo=bar”)

When I clck the required page say 2, the address bar changes to http://localhost/xxxxxx/product.asp?foo=bar&page=2

The page is not displayed as I get a page not found error.

23. Ajit Jadhav - December 29, 2009

Hello!
I am getting below
Microsoft VBScript runtime (0×800A000D)
Type mismatch: ‘CLng’

For page=CLng(Request(”page”))