Displaying alternating colors for table rows

# Thread: Displaying alternating colors for table rows

1. Senior Member
Join Date
Dec 1969
Posts
196

## Displaying alternating colors for table rows

I learned this in cold fusion, but am not sure how to do it in ASP. I believe I have to do a row count and then divide that by 2 and get the modulus, but don&#039t know how to write it in ASP. Can someone help me? Thanks in advance.

2. Senior Member
Join Date
Dec 1969
Posts
19,082

## RE: Displaying alternating colors for table rows

tutorial link : http://144.132.245.199/infinitemonkeys/<BR><BR>should be there, in the ASP section<BR><BR>j

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Displaying alternating colors for table rows

Well, not exactly an advanced question, but since you know how to do it in CF....<BR><BR>&#060;%<BR>bgColors = Array( "white", "yellow", "lightgreen", "pink" ) &#039 as many as you want!<BR>...<BR>...<BR>RowCount = 0<BR><BR>... set up the RS recordset ...<BR><BR>Do Until RS.EOF<BR>&nbsp; &nbsp; Response.Write "&#060;TR bgColor=&#039" & bgColors( RowCount MOD UBound(bgColors)+1 & "&#062;"<BR>&nbsp; &nbsp; ...<BR>&nbsp; &nbsp; ...<BR>&nbsp; &nbsp; RowCount = RowCount + 1<BR>&nbsp; &nbsp; RS.MoveNext<BR>Loop<BR>%&#062;<BR><BR>Yes, it works for only two colors in the array, if that is all you want.<BR><BR><BR><BR><BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
196

## RE: Displaying alternating colors for table rows

Using that article, here is how I put it into my code:<BR><BR>&#060;%<BR> If objRS.BOF or objRS.EOF THEN<BR> Response.Write "&#060;tr&#062;&#060;td&#062;&#060;p&#062;Ther e are currently no open issues.&#060;/p&#062;&#060;/td&#062;&#060;/tr&#062;"<BR> objRS.Close<BR> Set objRS = Nothing <BR> objConn.Close<BR> Set objConn = Nothing<BR> Else<BR> Do While Not objRS.EOF<BR>%&#062;<BR>&#060;%<BR> For x = 0 to 6<BR> If x mod 2 then<BR> bgcolor = "CCCCCC"<BR> Else<BR> bgcolor = "FFFFFF"<BR> End If<BR>%&#062;<BR>&#060;tr bgcolor="#&#060;%= bgcolor %&#062;"&#062;<BR>&#060;td&#062;<BR>&#060;b&#062;& #060;%=objRS("title")%&#062;&#060;/b&#062; - &#060;%=objRS("dDate")%&#062;<BR><BR>&#060;%=objRS ("news")%&#062;<BR>&#060;/td&#062;<BR>&#060;/tr&#062;<BR>&#060;%<BR> Next<BR> objRS.MoveNext<BR> Loop<BR>%&#062;<BR><BR>The result is giving me the first row, then 6 repeats of my first row without the body of the news, then my 2nd row, then 6 repeats of that, then my 3rd row, then 6 repeats of that. Can anyone help?

5. Senior Member
Join Date
Dec 1969
Posts
196

## RE: Displaying alternating colors for table rows

Bill, thanks, but I&#039m getting an all grey or all white with this code here:<BR><BR>&#060;%<BR> Dim bgColors<BR> bgColors = Array( "FFFFFF", "CCCCCC" )<BR> RowCount = 0<BR>&#039 Dim strSQL already done above<BR> strSQL = "SELECT * FROM tblNews WHERE status = &#039 1&#039 ORDER BY ID DESC"<BR>&#039 Dim objRS already done above<BR> Set objRS = Server.CreateObject ("ADODB.Recordset")<BR> objRS.Open strSQL, objConn<BR> Do While Not objRS.EOF<BR> Response.Write "&#060;tr bgColor=&#039" & bgColors( RowCount MOD UBound(bgColors)+1) & "&#039&#062;"<BR> Response.Write "&#060;td&#062;"<BR> Response.Write "&#060;b&#062;" & objRS("title") & "&#060;/b&#062; - " & objRS("dDate") & "<BR>"<BR> Response.Write objRS("news")<BR> Response.Write "&#060;/td&#062;"<BR> Response.Write "&#060;/tr&#062;"<BR> RowCount = RowCount + 1<BR> objRS.MoveNext<BR> Loop<BR>%&#062;

6. Senior Member
Join Date
Dec 1969
Posts
196

## This seems to be working

I tried this and it seems to be working...<BR><BR>&#060;%<BR> Response.Write "&#060;tr bgColor=&#039" & bgColors(RowCount MOD 2) & "&#039&#062;"<BR>%&#062;

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## ooops...missing parens...

Sorry!<BR><BR>This line:<BR><BR>Response.Write "&#060;tr bgColor=&#039" & bgColors( RowCount MOD UBound(bgColors)+1) & "&#039&#062;" <BR><BR>Need another set or parens, thus:<BR><BR>Response.Write "&#060;tr bgColor=&#039" & bgColors( RowCount MOD ( UBound(bgColors)+1) ) & "&#039&#062;" <BR><BR>But your way works fine, of course, because in your case the value of ( UBound(bgColors)+1) is (what else!) 2!<BR><BR>Actually, what you should probably do is something like:<BR><BR>&#060;%<BR>bgcolors = Array( "white","lightgrey" )<BR>bgCount = UBound(bgcolors) + 1 &#039 or just hard code the value, if you want<BR>...<BR>...<BR>Response.Write "&#060;tr bgColor=&#039" & bgColors( RowCount MOD bgCount ) & "&#039&#062;" <BR>...<BR>%&#062;<BR><BR>

#### Posting Permissions

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