Click to See Complete Forum and Search --> : How to alternate colors of rows when displaying data?


marksquall
04-20-2006, 01:43 AM
Dear Weddeveloper members,

A pleasant day to every one.

I'm currently working on this site that's using ASP for database connection. I have a link that when it's clicked, it will view all records in my MSAccess document (the file is list.asp). I just wonder how to alternate the colors of the row (I'm planning to have it simply white and light gray)as it loops until all the records are found.

These are the lines I'm working on:
<table>
<% while not rs.eof %>
<tr align="center" valign="top" bgcolor="#000000">
<td><%=rs.fields(1)%></td>
<td><%=rs.fields(2)%></td>
<td><%=rs.fields(3)%></td>
<td><%=rs.fields(4)%></td>
<td><%=rs.fields(5)%></td>
<td><%=rs.fields(6)%> </td>
</tr>
<%rs.movenext%>
<%wend%>
</table>

I hope I didn't erase important "details" on my table (I tried to delete my table formats so that everyone can see my code a little bit clear, hehehe).

So, if I have 20 records, then those codes will eventually generate 20 rows to display the records. I hope to get these rows have alternating colors so that is easy and clear to view such records. I hope you could help me.

Thanks everyone and more power.

Warm Regards,

MarkSquall

<Eddie>
04-20-2006, 06:39 AM
Try the code below:


<style type="text/css">
.highlight{background:#eee;}
</style>
<table>
<% while not rs.eof %>
<%
if blnHighlight=true then
blnHighlight=false
else
blnHighlight=true
end if
%>
<tr align="center" valign="top" bgcolor="#000000">
<td<% if blnHighlight=true then response.write(" class='highlight'") %>><%=rs.fields(1)%></td>
<td<% if blnHighlight=true then response.write(" class='highlight'") %>><%=rs.fields(2)%></td>
<td<% if blnHighlight=true then response.write(" class='highlight'") %>><%=rs.fields(3)%></td>
<td<% if blnHighlight=true then response.write(" class='highlight'") %>><%=rs.fields(4)%></td>
<td<% if blnHighlight=true then response.write(" class='highlight'") %>><%=rs.fields(5)%></td>
<td<% if blnHighlight=true then response.write(" class='highlight'") %>><%=rs.fields(6)%></td>
</tr>
<%rs.movenext%>
<%wend%>
</table>

What's happening is for every loop, the variable blnHighlight is changing its value which then dictates whether to apply a style to the row or not.

The code will need to be more efficient than above but you get the idea.

Ubik
04-20-2006, 09:13 PM
In your css, define these two styles:

Alternate0
{
bgcolor: CCCCCC;
}
Alternate1
{
bgcolor: DDDDDD;
}

Then, in your asp code:


intRowCounter = 1
while not rs.eof
response.write "<td class=""Alternate" & intRowCounter MOD 2 & """>" & rs.fielddata("fieldname") & "</td>"
intRowCounter = intRowCounter + 1
wend

marksquall
04-24-2006, 10:27 PM
Dear Mr. <Eddie> and Mr. Ubik,

I'm sorry if I didn't reply right away, I'm kind of busy one of these days. Any way, thank you for sharing your ideas to me...I'll try it on one of my webs I'm working, though I still not implementing it because my boss want to prioritize something else, but I'll apply it ASAP. Hope to hear from you again guys! :)


Best regards,


MarkSquall