Creating a Datagrid Class in classic ASP

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
%>


You might also like...

Comments

About the author

Brian O'Connell Ireland

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 appli...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perl - The only language that looks the same before and after RSA encryption.” - Keith Bostic