Alternating Row Colors In Your Query Results

A simple and effective way to jazz up your query results is to add some color.
All you're really doing is adding 6 lines of code to your query. These lines are:

<%
Count = 0
IF Count = 0 THEN
<td bgcolor="#f1f1f1"><% = RS(i) %></td>
<% Else %>
Count = Count + 1
If Count = 2 Then Count = 0
%>
Full Code
<%
Dim MyConn, sSql, RS, Count, i

Set MyConn=Server.CreateObject("ADODB.Connection")
MyConn.Open "demo"
sSql = "SELECT * FROM tblCustomer"
Set RS = MyConn.Execute(sSql)
%>

<table border=0>
<tr>
<% For i= 0 to RS.Fields.Count - 1 %>
<th bgcolor="#c0c0c0"><% = RS(i).Name %></th>
<% Next %>
</tr>

<% Count = 0 %>
'here you set the variable Count to zero
<% While Not RS.EOF %>
<tr>
<% for i= 0 to RS.Fields.Count - 1 %>
<% If Count = 0 Then %>
'check the value of Count. if Count = 0 then set the color to light gray
<td bgcolor="#f1f1f1"><% = RS(i) %></td>
<% Else %>
'otherwise, if Count does not equal zero then set the color to gray
<td bgcolor="#c0c0c0"><% = RS(i) %></td>
<% End If %>
<% Next %>
</tr>

<%
Count = Count + 1
'increment the variable Count
If Count = 2 Then Count = 0
'check the value of Count. if Count is greater than 1 then make it equal zero

RS.MoveNext
WEND
RS.Close
Set RS = Nothing
%>

</table>

colors recordset alternate row colors query asp


Back To Top
© 1998 - 2024 psacake.com
Version 7.21 | Advertise on this site