Tuesday, January 22, 2013

Classic ASP Tricks: Pagination control

Friends,

           If you are classic ASP programmer and you know ASP.NET as well then you might come to know that one of the beauty of ASP.NET over classic ASP is Microsoft's inbuilt as well as user defined controls.


        Now a days many companies are adopting ASP.NET technology or migrating their old classic ASP projects to ASP.NET. But nightmare classic ASP still exists in market and not going to disappear at least for next few years.


   You may face many challenges if you want to create or add any user defined controls like ASP.NET. No doubt it is nearly impossible. But we can mimic some controls like pagination.


     When I was working as classic asp programmer at that time I tried to write some controls for classic ASP and I got success in writing pagination control that make code much more readable, manageable and efficient. What you have to do is you just have to include pagination file, create instance of class, set it's properties like page size and call method to render pagination links. You don't have to bother about cursors and all.


     Now first of all create one header file and name it as pagination.asp. You can give any name to this file. I choose pagination.asp.


       Paste this code in same file.



<%
   'Designed and developed by Roshan Naralkar.
    Class Pagination
     
        Private adUseClient
        Private PSize 'Holds page size.
        Private RS 'Holds Recordset.
        Private BSize 'Block size.
        Private CacheSize 'Cache limit, always same as page size.
        Private TBlock 'Holds total no of blocks
        Private CBlock ' Hold current block.
        Private AbsPage 'Holds current page.
        Private StartPage 'Holds value of starting number of page in navigation bar
        Private EndPage 'Holds value of
     
        Public Sub Class_Initialize()
            PSize = 6
            BSize = 4
            adUseClient = 3
            CacheSize = PSize
         
            AbsPage = Trim(Request("page"))

            If isEmpty(AbsPage) OR isNull(AbsPage) OR (Not IsNumeric(AbsPage)) Then

                    AbsPage = 1
            End If
         
        End Sub
     
        '**********************************************
        'Set size i.e. number of record
        Public Property Let PageSize(size)
            If IsNumeric(size) Then
                PSize = size
                CacheSize = PSize
            End If
        End Property
     
        Public Property Get PageSize
            PageSize = PSize
        End Property
     
        '***************************************************
        'Property to set no of pages display in navigation
        Public Property Let BlockSize(size)
            If IsNumeric(size) Then
                BSize = size
            End If
        End Property
     
        Public Property Get BlockSize
            BlockSize = BSize
        End Property
     
        '*************************************************
        'Return size of total blocks. Using this property for internal purpose
        Private Property Get TotalBlock
            IF IsNull(RS) OR IsEmpty(RS) Then
                TBlock = 1
            Else
                TBlock = Int(rs.PageCount / BSize)
                If (rs.PageCount MOD BSize) <> 0 Then
                    TBlock = TBlock + 1
                End If
            End If
            TotalBlock = TBlock
        End Property
     
        '*************************************************
        'Returns current block
        Public Property Get CurrBlock
            If IsNull(RS) OR IsEmpty(RS) Then
                CBlock = 1
            Else
                CBlock = Int(AbsPage / BSize)
                If (AbsPage MOD BSize) <> 0 Then
                    CBlock = CBlock + 1
                End If
            End If
            CurrBlock = CBlock
        End Property
     
        '***************************************
        Public function BuildPagination(SQL, Conn)
            Set RS = CreateObject("ADODB.RecordSet")
            RS.CacheSize = PSize
            RS.PageSize = PSize
            RS.CursorLocation = adUseClient
         
            RS.Open SQL, Conn, 1, 3
            If Not (RS.EOF OR RS.BOF) Then
                RS.AbsolutePage = AbsPage
            End If
            Set BuildPagination = RS
        End Function
     
        '*************************************************
        'Rendering navigation to toggle between pages
        Public Sub BuildNav(QSTRING)
            If rs.PageCount > 1 Then
 
                StartPage = ((CurrBlock - 1) * BSize) + 1
 
                If CurrBlock <> TotalBlock Then
                    EndPage = StartPage + BSize
                Else
                    EndPage = rs.PageCount + 1
                End If
   
                If CurrBlock <> 1 Then
                    Response.Write "<a class='Prev' href='?page="&(StartPage - 1)&"&"&QSTRING&"'> << </a>&nbsp;"
                End If
 
 
 
                For i = StartPage To (EndPage - 1)
                    If Cint(i) <> Cint(AbsPage) Then
                        Response.Write "<a class='LinkPage' href='?page="&i&"&"&QSTRING&"'>"&i&"</a> "
                    Else
                        Response.Write " <span class='ActivePage'>" & i & "</span> "
                    End If
                Next

                If CurrBlock <> TotalBlock Then

                    Response.Write "<a class='Next' href='?page="&i&"&"&QSTRING&"'> >> </a> "
                End If
             End If
        End Sub
    End Class

%>


This is the class file that you have to include as header file where you want to render control. Now how to use it? Just follow these simple steps.


Step 1: 

          Initialize pagination class, Connection object an Recordset object. I'm using PGN object name for Pagination class, Oconn is my connection object an RS for Recorset.


  Set PGN = New Pagination

  Set rs = CreateObject("ADODB.RecordSet")

  PGN.PageSize = 10

  PGN.BlockSize = 3

  Set rs = PGN.BuildPagination("[Groups]",oconn)


       Now read this code carefully. Here we initialize PGN class and then we set properties PageSize and BlockSize. You may have understand PageSize is number of records per page, then what is BlockSize?

BlockSize is how many pages should display inside page navigator. e.g. If you have total 10 pages and you set block size 3 then control will render navigation links as << 1 2 3...>> means only 3 links will appear at a time.

      PGN object has BuildPagination method. It accepts table name or SQL query  and connection object as arguments. BuildPagination returns recordset object



Step 2: Loop through records to print on screen.


Initialize two variables to loop through each row of recordset object.



iStart = 1

iEnd = iStart + PGN.PageSize


While Not ((iStart = iEnd) OR (rs.EOF))

     Response.Write RS("ColumnName") & "</br>"

     rs.MoveNext

    iStart = iStart + 1
  Wend


   

You may use table tags to print record in grid.

Step 3: Print Navigation links


To print navigation links you just have to call PGN.BuildNav

PGN.BuildNav takes 1 argument that is querystring. You can set parameter to blank string if you don't have any query string to pass.

You can call method as

PGN.BuildNav("") 
or
PGN.BuildNav("ID=1&Name=ABC")

Now that's it.


Whole source code is as follows. You can refer that




<%
 
  Set PGN = New Pagination
  Set rs = CreateObject("ADODB.RecordSet")

  PGN.PageSize = 2

  PGN.BlockSize = 3


  Set rs = PGN.BuildPagination("[Groups]",oconn)


  Response.Write rs("groupname") & " : " & PGN.PageSize & " : " & TypeName(rs)

  iStart = 1
  iEnd = iStart + PGN.PageSize
 
  Response.Write "<hr />"

  While Not ((iStart = iEnd) OR (rs.EOF))

    Response.Write "<br>"&rs("groupname")&" POS : "& rs.AbsolutePosition
    rs.MoveNext
    iStart = iStart + 1
  Wend

  Response.Write "<hr/>"


  PGN.BuildNav("")

%>


No comments:

Post a Comment