Use ASP's Mod operator for greenbar tables
Despite the fact that we've just moved into a new millenium, many of your clients 
still may want to see the old style green barreports even in a Web page. 
The trick to this method is alternating each row's background color. To do so, your 
code must in essence know if it's on an odd or even row and format it accordingly.
ASP's Mod operator offers the perfect way to do so. As you may know, this operator 
divides two numbers and returns the remainder. So if you use, say, 3 Mod 2, 
the expression returns 1. 4 Mod 2 would return 0. For greenbar tables, our script 
toggles the row color on and off depending on the result of a Mod expression.
To illustrate, we modified the code from a previous tip, below. 
 
<%@ LANGUAGE="VBSCRIPT" %>
<%
Dim objConn, objRec, strTable, strCmd, strColor
strCmd = "SELECT * FROM Authors"
Set objConn=Server.CreateObject("ADODB.Connection")
Set objCmd = Server.CreateObject("ADODB.Command")
set objRec = Server.CreateObject("ADODB.Recordset")
objConn.Open "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" & _
     Server.MapPath("Biblio.mdb")
objRec.open strCmd, objConn, , 1, 1
sTable="<TABLE BORDER='1'><THREAD><TR>"
'Collect the Field Names
For Each fld in objRec.Fields
         sTable = sTable & "<TD>" & fld.Name & "</TD>"
Next
'Build the table body
Do While Not objRec.EOF
  X = X + 1
  If X Mod 2 then
    strColor = "00ffff"
  Else
    strColor = "#ffffff"
  End If	
 
  sTable = sTable & "<TR bgcolor='" & strColor & "'>"
  For Each fld in objRec.Fields
    sTable = sTable & "<TD><font size = '2'>" & _
       fld & "</font></TD>"
  NextobjRec.MoveNext
sTable = sTable & "</TR>"
loop
Response.write sTable
objRec.Close
Set objRec = Nothing
objConn.Close
Set objConn = Nothing%>
 
 


Active Server Index

Main Index

Search RD Techbase