...

View Full Version : Alternate row colors in asp with existing code...



anessa05
03-07-2006, 08:49 PM
I need to display every other row from a sql db in an asp page in grey. I've seen plenty of examples from scratch, but I need to use the existing code and incorporate the display tweek somehow. Could someone show me how please? Here's my code...


Sub displaySelectedJobs()
if rs.eof then '## if no jobs match selected criteria
Response.Write "<p>No jobs posted that match your criteria<p>"
else
do while not rs.eof '##### iterate entire data set
%>
<tr align="left" valign="top">
<td>
<a href="displayJob.asp?jobId=<%=rs("jobId")%>"><%=rs("jobId")%></a>
</td>
<td>
<%= rs("postDate")%>
</td>
<td>
<%= rs("typeName")%>
</td>
<td>
<%= rs("state")%>
</td>
</tr>
<%
rs.moveNext
loop
end if
End Sub

TheShaner
03-07-2006, 11:10 PM
It's just a matter of keeping track which row you're on. There are various methods. I could've just kept a constant count and then displayed a bgcolor on the TR whenever count % 2 didn't equal 0 (whenever count was odd). I just thought this would be the easiest to understand. The changes are in red. All I did was create a sort of switch that just switches back and forth between 0 and 1. 0 displays no bgcolor and 1 displays a gray bgcolor.

<%
Sub displaySelectedJobs()
If rs.EOF Then '## if no jobs match selected criteria
Response.Write "<p>No jobs posted that match your criteria<p>"
Else
Dim bg_switch
bg_switch = 0
Do While Not rs.EOF '##### iterate entire data set
If bg_switch = 0 Then
%>
<tr align="left" valign="top">
<%
bg_switch = 1
Else
%>
<tr bgcolor="#CCCCCC" align="left" valign="top">
<%
bg_switch = 0
End If
%>
<td>
<a href="displayJob.asp?jobId=<%=rs("jobId")%>"><%=rs("jobId")%></a>
</td>
<td>
<%= rs("postDate")%>
</td>
<td>
<%= rs("typeName")%>
</td>
<td>
<%= rs("state")%>
</td>
</tr>
<%
rs.MoveNext
Loop
End If
End Sub
%>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum