Pagination Script in ASP

Posted on Aug 20, 2011 in ASP
Pagination Script in ASP

'    iNumPerPage is the number of items to
' display on each page                    
'    sURL is the page to put with the link
' , sQuerystring is additional qs stuff    
'    adodbConnection, adodbCommand, sTable
' are all for the table to get row count from.                                                            *
'                                                                                *
'    Print out the numbers (if any) betwee
' n the "previous" and "next" buttons    
'    It'll act like this (current # is in 
' bold):                                 
'    1 2 3 4 5 6 7 8 9 <b>10</b> >> next   
'    previous << <b>11</b> 12 13 14 15 16 17 18                                    *
'                                                                                *
'' ****************************************

Sub PrintRecordsetNav( iNumPerPage, adodbConnection, adodbCommand, sTable, sURL, sQuerystring )
    
    Dim iTtlNumItems, iDBLoc, sSqlTemp, iTtlTemp
    Dim iDBLocTemp, sURLBeg, iA, iB, x, iTemp, rsObj
    
    iDBLoc = CInt(Request("iDBLoc"))
    iTtlNumItems = CInt(Request("iTtlNumItems"))
    ' Get ttl num of items from the database if it's Not already In the QueryString
    If (iTtlNumItems = 0) Then
        Set rsObj = Server.CreateObject("ADODB.Recordset")
        sSqlTemp = "SELECT COUNT(*) FROM " & sTable
        adodbCommand.CommandText = sSqlTemp
        rsObj.Open adodbCommand
        If Not(rsObj.EOF) Then
            iTtlNumItems = rsObj(0)
        End If
        rsObj.Close
        Set rsObj = Nothing
    End If
    iTtlTemp = iTtlNumItems  iNumPerPage   ' this is the number of numbers overall (use the "" To return int)
    iDBLocTemp = iDBLoc  iNumPerPage       ' this is which number we are currently On (use the "" To return int)
    If (sQuerystring <> "") Then
        sURLBeg = "<A href = """ & sURL & "?" & sQuerystring & "&iTtlNumItems=" & iTtlNumItems & "&iDBLoc="
    Else
        sURLBeg = "<A href = """ & sURL & "?iTtlNumItems=" & iTtlNumItems & "&iDBLoc="
    End If
    
    '***** BEGIN DISPLAY *****
    ' Print the "Previous"
    If (iDBLoc <> 0) Then
        Response.Write sURLBeg & (iDBLoc - iNumPerPage) & """>Previous</A> "
    End If
    ' Print the <<
    If (iDBLocTemp >= iNumPerPage) Then
        Response.Write sURLBeg & (( iDBLocTemp  iNumPerPage ) * iNumPerPage ^ 2) - (iNumPerPage * 9) & """><<</A> "
    End If
    
    ' Print the numbers in between. Print them out in sets of 10.
    iA = ( iDBLocTemp  iNumPerPage ) * iNumPerPage
    iB = ( iDBLocTemp  iNumPerPage ) * iNumPerPage + iNumPerPage
    For x = iA To iB
        iTemp = (x * iNumPerPage)
        If (iTemp < iTtlNumItems) Then  ' takes care of extra numbers after the overall final number
            If (iDBLoc = iTemp) Then
                Response.Write " <B>[" & x+1 & "]</B>"
            Else
                Response.Write " " & sURLBeg & (x * iNumPerPage) & """>" & x+1 & "</A>"
            End If
        Else
            Exit For
        End If
    Next
    
    ' Print the >>
    If (iTtlTemp > iDBLocTemp) Then
        If ((iDBLocTemp + iNumPerPage) <= iTtlTemp) Then
            Response.Write " " & sURLBeg & (( iDBLocTemp  iNumPerPage ) * iNumPerPage + iNumPerPage ) * iNumPerPage & """>>></A> "
        End If
    End If
    ' Print the "Next"
    If ((iDBLoc + iNumPerPage) < iTtlNumItems) Then
        Response.Write " " & sURLBeg & (iDBLoc + iNumPerPage) & """>Next</A>"
    End If
    '***** End DISPLAY *****
    
End Sub