Paging with Alternating Colors

This script will take the results of a query and sets up number of records per page (set at 10 currently) and alternates the colors of each record and provides a link to get the next 10 results.

<%@language=vbscript%>
<%
'Create the MyConnection Object and Open the connection to the database using your DSN
Set MyConnection = Server.Createobject("Adodb.Connection")
MyConnection.open "DSN=DSNtest;Username=admin;Password=password;"

'Write your Query
MyQuery = "Select FieldName from TableName"
perpage = 10
  
const adOpenForwardOnly = 0
const adOpenKeyset = 1
const adOpenDynamic = 2
const adOpenStatic = 3
const adLockReadOnly = 1
const adLockPessimistic = 2
const adLockOptimistic = 3
const adLockBatchOptimistic = 4
set myrs = server.createobject("adodb.recordset")
myrs.open myquery, MyConnection, adopenkeyset, adlockoptimistic
if myrs.eof then
response.write "<table cellspacing=0 border=0 cellpadding=0 border=0 width=640 bgcolor=black><tr><td><table cellspacing=1 border=0 cellpadding=2 border=0 width=100% bgcolor=black><tr><td align=center bgcolor=Gray><font face=""Verdana"" size=1>Sorry, no records were found.</font></td></tr></table></td></tr></table>"
elseif request("start") = "" then
start = 1
else
start = cint(request("start"))
end if

  torecs = ((start+perpage)-1)

  if torecs > myrs.recordcount then
  torecs = myrs.recordcount
  end if

  i = 1
  mypagenavigationbar = ""
  for n = 1 to myrs.recordcount step perpage
if n <> 1 then mypagenavigationbar = mypagenavigationbar & " | "
    if n = cint(start) then
    mypagenavigationbar = mypagenavigationbar & "<b>"&i&"</b>"
   else
    mypagenavigationbar = mypagenavigationbar & "<a href="""
    mypagenavigationbar = mypagenavigationbar & request.servervariables("url") & "?start=" & n
    mypagenavigationbar = mypagenavigationbar & """>"
    mypagenavigationbar = mypagenavigationbar & i & "</a>"
  end if
  i = i + 1
  next
  i = 1
 myrs.move start - 1
Response.Write "<table cellspacing=0 border=0 cellpadding=0 border=0 width=640 bgcolor=black><tr><td><table cellspacing=1 border=0 cellpadding=2 border=0 width=100% bgcolor=black>"
Response.Write "<tr><td bgcolor=Gray align=right><font face=""Verdana"" size=1><b>Records </b> ("&start& "-" & torecs & " of " & myrs.recordcount&")  <b>Page(s)</b> : "&mypagenavigationbar&"</font></td></tr>"
Response.Write "</table></td></tr><tr><td><table cellspacing=1 border=0 cellpadding=2 border=0 width=640 bgcolor=black><tr bgcolor=Silver>"
response.write "<td align=Left><font face=""Verdana"" size=1>FieldNameHeading</font></td>"

Response.Write "</tr>"

iscolor = true
for row = start to start + (perpage - 1)
if myrs.eof then exit for
if iscolor then
response.write "<tr bgcolor=Olive>"
iscolor=false
else
response.write "<tr bgcolor=Purple>"
iscolor=true
end if
response.write "<td align=Left><font face=""Verdana"" size=1>"& Myrs("FieldName")& " </font></td>"

response.write "</tr>"
i = i + 1
myrs.movenext
next

Response.Write "</table></td></tr></table>"
'Close the Recordset
myrs.close
set myrs = nothing

'Close the Connection
MyConnection.close
Set MyConnection = Nothing
%>

Views 137 Downloads 62

Perm link


JeffSmith
26
Graphics Classic ASP
Revisions

v1.0