Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 72,317 times

Contents

Related Categories

Creating a Datagrid Class in classic ASP - Creating the class

musician

Creating the class

I'm assuming you have heard of classes in vbscript and know a little about OOP. Lets have a look at the class, discuss it and then outline some possible improvements:-

<%
Class caDataGrid
   'private variables
   private pAutoColumns, pConnStr, pSqlStr, intColCnt
   Private pOutPut, pConn, pRec, x, y, pArray

   'this runs when you create a reference to the caDataGrid class
   Private Sub Class_Initialize()
       Set pConn = server.createobject("adodb.connection")
       Set pRec = server.createobject("adodb.recordset")
       intColCnt = 0
       pAutoColumns = True
   End Sub
   
   'Properties - all writable
   Public Property Let ConnectionString(strConn)
       pConnStr = strConn
   End Property

   Public Property Let AutoColumns(bAutoCols)
       If bAutoCols = True or bAutoCols = False then
           pAutoColumns = bAutoCols
       End IF
   End Property

   Public Property Let SqlString(strSql)
       pSqlStr = strSql
   End Property

   'Methods for our class
   Public Sub AddColumn(strColName)
       If intColCnt = 0 then
           pOutPut = "<table width='100%' border=1 cellpadding=0 cellspacing=0>" & vbcrlf
           pOutPut = pOutPut & "<tr>" & vbcrlf
       End If
       pOutPut = pOutPut & "<td><strong>" & strColName & "</strong></td>" & vbcrlf
       intColCnt = intColCnt + 1
   End Sub
   
   Public Sub Bind
       pConn.Open pConnStr
       Set pRec = pConn.Execute(pSqlStr)
       If pAutoColumns = True then
           'assign column names from returned recordset
           pOutPut = "<table width='100%' border=1 cellpadding=0 cellspacing=0>" & vbcrlf
           pOutPut = pOutPut & "<tr>" & vbcrlf
           Redim pColNames(pRec.Fields.Count)
           For x = 0 to pRec.Fields.Count - 1
               pOutPut = pOutPut & "<td>" & pRec.Fields(x).Name & "</td>" & vbcrlf
           Next
       End If
       pOutPut = pOutPut & "</tr>" & vbcrlf
       pArray = pRec.GetRows
       For x = 0 to UBound(pArray, 2)
           pOutPut = pOutPut & "<tr>" & vbcrlf
           For y = 0 to UBound(pArray, 1)
   pOutPut = pOutPut & "<td>" & pArray(y, x) & "</td>" & vbcrlf
           Next
           pOutPut = pOutPut & "</tr>" & vbcrlf
       Next
       pOutPut = pOutPut & "</table>" & vbcrlf
       Response.Write pOutPut
   End Sub
   
   'this runs when we destroy our reference to caDataGrid
   Private Sub Class_Terminate()
       pOutPut = ""
       pRec.Close
       Set pRec = nothing
       pconn.close
       Set pConn = nothing
   End Sub

End Class
%>


Microsoft Certified Applications Developer with 10 years experience developing web based applications using asp, asp.net for a Local Authority in Dublin. Clings to a firm belief that a web application must keep it's 3 most important aspects seperate - presentation (external CSS), structure (XHTML) and behavior (external javascript).

Comments

  • Used this class as a base for a DropDownMenu class

    Posted by dibley1973 on 05 Aug 2006

    What a great article!!! I have just been starting to use .Net at work but still use classic ASP for home projects so liked the similarity of a near OO approach. I needed a DropDown...

  • formatting the content of an automatically generat

    Posted by trish on 11 Aug 2005

    Did you ever find out on how to do this..........? I'm try to accomplish the same...

    Thanks

  • Quick Improvement Tip...

    Posted by ChristianCalderon on 05 Apr 2004

    When looping and printing from a recordset it is better to get a reference to the fields that we want to print.
    For instance, this code would be more efficient (specially true when printing large set...

  • formatting the content of an automatically generat

    Posted by lotiejam on 26 Feb 2003

    I have used the datagrid class code to great success but would like to format one of the columns so the data displayed is can be used as a link(url). Each url would point to the same page and session ...

  • Datagrid class

    Posted by moose on 04 Jul 2002

    I thought the class was groovy. It will save a lot of time setting up data driven pages.

    Well done mate!

    I've ammended the class to parameterise the table formatting. Very easy to do.
    Thanks fo...