Print Page | Close Window

Alternating Table Colors

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=27055
Printed Date: 29 March 2026 at 4:24am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Alternating Table Colors
Posted By: kennywhite
Subject: Alternating Table Colors
Date Posted: 02 March 2009 at 7:05pm
Hello,
 
I looked online for a few examples of how to do this, but I've not been able to find anything that doesn't break my page or simply doesn't work at all. Can anyone tell me how to properly do this? I'd like for every other 
<tr>
tag to be a different color.
 
Here is my code currently:
 

 
<html>
<head>
<title>Assets</title>
</head>
<body bgcolor="white" text="black">
<table cellspacing="0" cellpadding="0" border="1" width="100%">
<tr>
<th align="center" width="150" bgcolor="gray"><b><u>DEVICE TYPE</U></B></th>
<th align="center" width="100"  bgcolor="gray"><b><u>MODEL #</U></B></th>
<th align="center" width="150"  bgcolor="gray"><b><u>SERIAL #</U></B></th>
<th align="center" width="150"  bgcolor="gray"><b><u>DEPARTMENT</U></B></th>
<th align="center" width="150"  bgcolor="gray"><b><u>USER</U></B></tdh>
<th align="center" width="110"  bgcolor="gray"><b><u>CONDITION</U></B></th>
<th align="center" width="250"  bgcolor="gray"><b><u>NOTES</U></B></th></TR>
<%
'Dimension variables
Dim adoCon         'Holds the Database Connection Object
Dim rsAsset    'Holds the recordset for the records in the database
Dim strSQL         'Holds the SQL query to query the database
 
'Create an ADO connection object
Set adoCon = Server.CreateObject("ADODB.Connection")

 'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("assets.mdb")
 'Create an ADO recordset object
Set rsAsset = Server.CreateObject("ADODB.Recordset")
'Initialise the strSQL variable with an SQL statement to query the database DESCending order
strSQL = "SELECT assets.DeviceType, assets.Model, assets.Serial, assets.Department, assets.User, assets.Condition, assets.Notes FROM assets ORDER BY assets.ID_no DESC;"
 
 

'Open the recordset with the SQL query
rsAsset.Open strSQL, adoCon
%>
 

<%
'Loop through the recordset
Do While not rsAsset.EOF
    'Write the HTML to display the current record in the recordset
 
Response.Write ("<tr>")
Response.Write ("<td>")   
    Response.Write (rsAsset("DeviceType"))
Response.Write ("</td>")
Response.Write ("<td>")
    Response.Write (rsAsset("Model"))
Response.Write ("</td>")
Response.Write ("<td>")
Response.Write (rsAsset("Serial"))
Response.Write ("</td>")
Response.Write ("<td>")
Response.Write (rsAsset("Department"))
Response.Write ("</td>")
Response.Write ("<td>")
Response.Write (rsAsset("User"))
Response.Write ("</td>")
Response.Write ("<td>")
Response.Write (rsAsset("Condition"))
Response.Write ("</td>")
Response.Write ("<td>")
    Response.Write (rsAsset("Notes"))
Response.Write ("</td>")
Response.Write ("</tr>")
 
    'Move to the next record in the recordset
    rsAsset.MoveNext
Loop
 
'Reset server objects
rsAsset.Close
Set rsAsset = Nothing
Set adoCon = Nothing
%>
</table>
</body>
</html>
 
 
 
Thanks in advance!



Replies:
Posted By: Scotty32
Date Posted: 02 March 2009 at 8:29pm
Add at the start of the loop:

Do While not rsAsset.EOF 

if iLoop = 2 then iLoop = 0

if iLoop = 1 then
   strColour = "#99999"
else
  strColour = "#EEEEE"
end if



add to the row

Response.Write ("<tr bgcolor=""" & strColour & """>")



Add at the end of the loop

    iLoop = iLoop + 1

    rsAsset.MoveNext
Loop



You can easily change it to CSS if you wish.


-------------
S2H.co.uk - http://www.s2h.co.uk/wwf/" rel="nofollow - WebWiz Mods and Skins

For support on my mods + skins, please use http://www.s2h.co.uk/forum/" rel="nofollow - my forum .


Posted By: kennywhite
Date Posted: 03 March 2009 at 2:17pm
Thumbs Up
 
THANKS!
 
This is by far the most helpful forum I've ever joined!



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net