Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alternate row color from DB query

Status
Not open for further replies.

MDA

Technical User
Jan 16, 2001
243
US
Hi all...

I have an asp page that pulls in a list from a SQl db. my goal is to format the table for easy reading. I would like to alternate the rows colors from White to Grey. Any ideas on how I could do this?

FYI: the code I am using is:
-----------------------------
<P>
<TABLE BORDER=1>
<TR>
<% For i = 0 to RS.Fields.Count - 1 %>
<TD><B><%= RS(i).Name %></B></TD>
<% Next %>
</TR>
<% DO While Not RS.EOF %>
<TR>
<% For i = 0 to RS.Fields.Count - 1 %>
<TD VALIGN=TOP>
<%= RS(i) %>
</TD>
<% Next %>
</TR>
<%
RS.MoveNext
Loop
RS.Close
OBJdbConn.Close
Set OBJdbConn = Nothing
%>
</Table>
---------------------------------

Thanks in advance for any help...

Regards,

MDA
 
Set a variable called WhatColor = &quot;W&quot; (for white). Now write an if statement that basically says:

<%If WhatColor = &quot;W&quot; then
WhatColor = &quot;G&quot;%>
<td bgcolor=White>
<%Else
WhatColor = &quot;W&quot;%>
<td bgcolor=Grey>
<%End If%>

This should alternate the colors between rows.

Hope that helps...
mwa
 
You can also use the MOD function for something like this:

<%
dim x, fieldStyle
%>
<P>
<TABLE BORDER=1>
<TR>
<% For i = 0 to RS.Fields.Count - 1 %>
<TD><B><%= RS(i).Name %></B></TD>
<% Next %>
</TR>
<%
x = 1
DO While Not RS.EOF %>
<TR>
<%
if x MOD 2 = 0 then
fieldStyle = &quot;BGCOLOR='GREY'&quot;
else
fieldStyle = &quot;BGCOLOR='WHITE'&quot;
end if

For i = 0 to RS.Fields.Count - 1 %>
<TD <%=fieldStyle%> VALIGN=TOP>
<%= RS(i) %>
</TD>
<% Next %>
</TR>
<%
RS.MoveNext
Loop
RS.Close
OBJdbConn.Close
Set OBJdbConn = Nothing
%>
</Table>
 
Sorry, I forgot to increment the x!

<%
dim x, fieldStyle
%>
<P>
<TABLE BORDER=1>
<TR>
<% For i = 0 to RS.Fields.Count - 1 %>
<TD><B><%= RS(i).Name %></B></TD>
<% Next %>
</TR>
<%
x = 1
DO While Not RS.EOF %>
<TR>
<%
if x MOD 2 = 0 then
fieldStyle = &quot;BGCOLOR='GREY'&quot;
else
fieldStyle = &quot;BGCOLOR='WHITE'&quot;
end if

For i = 0 to RS.Fields.Count - 1 %>
<TD <%=fieldStyle%> VALIGN=TOP>
<%= RS(i) %>
</TD>
<%
x = CInt(x) + 1
Next %>
</TR>
<%
RS.MoveNext
Loop
RS.Close
OBJdbConn.Close
Set OBJdbConn = Nothing
%>
</Table>
 
<% Counter = 1 %>
<% put your loop code here %>
<% If Counter Mod 2 = 0 Then %>
<tr bgcolor=&quot;#A7B4D8&quot;>
<% Else %>
<tr bgcolor=&quot;#DDE4F0&quot;>
<% End If %>
<td>blah blah blah<td>
</tr>
<% put your looping end code here and add one to the counter value.
Counter = Counter + 1
Wend%>
 
A similar method is to define a two value array and use the mod as index
Code:
Dim colors(1), ctr
ctr = 0
colors(0) = &quot;#eeeeee&quot;
colors(1) = &quot;#ccccee&quot;

Response.Write &quot;<table>&quot;
Do While Not rs.EOF
   Response.Write &quot;<tr style='background-color:&quot;&colors(ctr mod 2)&&quot;;'>&quot;
   'your <td>s and </tr> here
   ctr = ctr + 1
   rs.MoveNext
Loop
Response.Write }</table>

Or to define two row styles named rwo0 and row1 and just concatenate the mod'd value to the class name.
-Tarwn ------------ My Little Dictionary ---------
Extreme Programming - (1)Trying to code before my second cup of coffee. (2) While(1){ Ctrl+C; Ctrl+V; }
FAQ - Web-ese for &quot;Forget Asking Questions, I am to busy&quot; :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top