You are on page 1of 4

Home

Visual Basic 6

Tutorials

Creating a Datagrid Class in classic ASP
30 Apr 2002 | by Brian O'Connell | Filed in ASP Visual Basic 6 Comments PDF

Introduction
Well we've all heard of the wonderful Datagrid in ASP.Net and it's pretty handy to say the least. Then again some of you like me may still be stuck with poor old ASP for the moment. But it's not all bad. With vbscript classes in ASP you can acomplish many things. So I got the urge to try and create my own version of the datagrid. To create a DataGrid class my first problem was efficiency. When learning how to get records from a database and display them in a table on your asp page you'll quite often do it like this:-

<table> <% Do Until objRec.EOF %> <tr> <td><%=objRec("name") %></td> <td><%=objRec("address") %></td> <td><%=objRec("email") %></td> </tr> <% objRec.movenext loop %> </table>

This is quite an innefficient way of doing things. A more efficient way of doing things is to use the RecordCount property of the Recordset, for x = 0 to objRec.RecordCount.

2-Dimensional Arrays
Even more efficient is to use the GetRows method of the Recordset to populate a 2 dimensional array. Then you no longer have to use up resources accessing the Recordset.

dim arTable arTable = objRec.GetRows objRec.Close set objRec = nothing

You now have a 2 dimensional array but how is the recordset information stored in this array? arTable's 2 dimensions are like this: arTable(TotalCols, TotalRows). So if my Recordset returned 10 records of the above fields it would be arTable(3, 10) and you would need to go from arTable(0, 0) to arTable(2, 9) to retreive each value and you must go through the TotalCols elements first, followed by the TotalRows element. You don't know how many records you will return so you need to use the UBound function to evaluate them:-

dim tCols, tRows tCols = UBound(arTable, 1) tRows = UBound(arTable, 2)

With an array of more than one dimension you need to supply the UBound function with a value to indicate which element you wish to total. As above we set tCols to the total of the first dimension and tRows to the total of the second dimension. With this information you can now loop through the records using nested For Next loops:-

<table> <% Dim x, y For x = 0 to tRows Response.Write "<tr>"

Count) For x = 0 to pRec. pConnStr.Name & "</td>" & vbcrlf Next End If pOutPut = pOutPut & "</tr>" & vbcrlf pArray = pRec.Write pOutPut End Sub 'this runs when we destroy our reference to caDataGrid Private Sub Class_Terminate() pOutPut = "" pRec.Write "<td>" & rTable(y. Lets have a look at the class. 2) pOutPut = pOutPut & "<tr>" & vbcrlf For y = 0 to UBound(pArray. private variables and code for when the class is first instantiated. Creating the class I'm assuming you have heard of classes in vbscript and know a little about OOP.1 pOutPut = pOutPut & "<td>" & pRec. pConn. discuss it and then outline some possible improvements:<% Class caDataGrid 'private variables private pAutoColumns.recordset") intColCnt = 0 pAutoColumns = True End Sub 'Properties .connection") Set pRec = server. intColCnt Private pOutPut. pRec.Open pConnStr Set pRec = pConn. pSqlStr.createobject("adodb.Write "</tr>" Next %> Not as simple as directly referring to field names in the recordset object but faster and the code is hardly more complicated.Fields(x).k. y.Fields. x) & "</td>" & vbcrlf Next pOutPut = pOutPut & "</tr>" & vbcrlf Next pOutPut = pOutPut & "</table>" & vbcrlf Response.Count .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.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.close Set pConn = nothing End Sub End Class %> Analysis of the Class O. pArray 'this runs when you create a reference to the caDataGrid class Private Sub Class_Initialize() Set pConn = server. first of you declare the class. x.Close Set pRec = nothing pconn. 1) pOutPut = pOutPut & "<td>" & pArray(y.For y = 0 to tCols Response.Fields.createobject("adodb. x) & "</td>" Next Response. As I said you must understand what is involved in making a class or you might be a bit confused:Class caDataGrid .GetRows For x = 0 to UBound(pArray. So now wehave a good model which we can move/encapsulate in a handy vbscript class.

or not if you use AutoColumns.'private variables private pAutoColumns. These add the actual functionality to the 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 If we choose to specify our own column names then we call this method to add a column name.createobject("adodb.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 I haven't included code for reading these values. We only want to start the table and the row if it's at 0. pSqlStr. pArray 'this runs when you create a reference to the caDataGrid class Private Sub Class_Initialize() Set pConn = server. pRec. it's on to the Binding of the grid:- Public Sub Bind pConn.Fields(x). i.recordset") intColCnt = 0 pAutoColumns = True End Sub When the Class initializes we set our connection object and recordset object as adodb objects. Otherwise in this case it remains true from the initializing code. the Methods. pConnStr.1 pOutPut = pOutPut & "<td>" & pRec. I'm skimping a bit on error checking but the procedure to set the AutoColumns boolean checks to make sure only true or false is passed to it.connection") Set pRec = server.Count . intColCnt Private pOutPut. procedures. 'Public Property Get'. Once thats done.Count) For x = 0 to pRec. Every other time we want to just add the cell code.e. It's in this procedure you would put any defaults you want for your class. It just adds a new cell to our grid(table) for each column and this is where the column count comes in. for example. We also set the Column Count variable to 0 for use when we want to define our own columns and finally we default to AutoColumns=true. for your class. It's a good idea to include more complete error code for these procedures to ensure proper values are set and especially if you wanted to make your class widely available.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. x.Fields.Open pConnStr Set pRec = pConn. More Analysis of the Class Now onto the meat and veg. Next we create the procedures that allow us to set the various proerties:'Properties . as we don't really need to do this.createobject("adodb. y. pConn.Fields.Name & "</td>" & vbcrlf .

0. On the more interesting side of things I could add alot of similar stuff to this class as you find with the ..Jet.SqlString = "select firstname. Anyway hope this is of use to you all.MapPath(mapPath) cDataGrid.ConnectionString = "Provider=Microsoft. Until tomorrow when you install the . x) & "</td>" & vbcrlf Next pOutPut = pOutPut & "</tr>" & vbcrlf Next pOutPut = pOutPut & "</table>" & vbcrlf Response. Happy Coding. I use the nwind. title from employees" cDataGrid. Of course theres nothing to say you can't just paste your class code into the same page but it wouldn't be nice and neat would it? Then you declare a variable and set it as a caDataGrid object/class. Now create an asp page in your wwwroot folder with the following:<%@ LANGUAGE="VBSCRIPT" %> <% option explicit %> <% response. background colours or even class names from a stylesheet all with some more properties and methods. All of this is being concatenated to an output string which is finally written to the browser at the end.paging. 2) pOutPut = pOutPut & "<tr>" & vbcrlf For y = 0 to UBound(pArray.Write pOutPut End Sub Basically we open our recordset.ConnectionString = "Provider=Microsoft.GetRows For x = 0 to UBound(pArray.SqlString = "select firstname.AutoColumns = false cDataGrid. lastname. Using the Class in an asp page Paste the class code into notepad and save it as caDataGrid.Jet.mdb" 'Access 2000 connection cDataGrid.MapPath(mapPath) cDataGrid. in my case in my Visual Basic folder.AddColumn("Job Title") cDataGrid.AutoColumns = false and then forget to add column names then I'd have get bad output. then we use the code already discussed to cycle through our array of values. If you have been paying attention you might have realised how much more you could do with this.net framework and forget all about it.asp"--> <body> <% dim cDataGrid.OLEDB.AddColumn("First Name") cDataGrid.4. 1) pOutPut = pOutPut & "<td>" & pArray(y.buffer=true %> <html> <head> <title>caDataGrid Test</title> </head> <!-. title from employees" cDataGrid.OLEDB.Bind set cDataGrid = nothing %> </body> </html> You 'reference' the class using the include capability of asp. You might also like.0. Data Source="& Server. mapPath Set cDataGrid = New caDataGrid mapPath = "nwind.net datagrid .Bind set cDataGrid = nothing 'now lets set our own columns response. assign the relevent values and bind. Very handy.Next End If pOutPut = pOutPut & "</tr>" & vbcrlf pArray = pRec. I copied the database to the wwwroot folder for simplicity. More work obviously but once it's done it'll be there to use many many times.4. if AutoColums=true then we get the fields names and create cells for them or we go with the custom column names. First on the list should be better error code. Now lets use the class in an asp page.AddColumn("Last Name") cDataGrid. All of 5/6 lines of code. © 1999-2012 Developer Fusion Ltd Web hosting by UKFast .. Data Source="& Server.asp in your wwwroot folder.mdb database as an example here which is found with plenty of MS products. For example if I set cDataGrid.write "<br><br>" Set cDataGrid = New caDataGrid cDataGrid.#include file="caDataGrid. lastname.