Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Alternate row colors in asp with existing code...

    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...
    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

  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    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.
    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
        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
    %>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •