Learn how to alternate row colors from database output using ASP.Simple enough, lets decide what this is going to do in English:
- Connect to the database, execute the SQL query.
- Run an IF...ELSE statement and use MOD 2 = 0 to figure whether the variable oddsevens is odd or even.
- Display a background color of black or white, depending on whether oddsevens was odd or even.
- Display the data.
- Add one to oddsevens, so the next row will be an alternating color.
- Move to the next row in the SQL query, continue the DO LOOP.
<%
Dim Conn
Dim SqlTemp
Dim oddsevens
oddsevens = 1
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.connectionstring =
"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" & Server.MapPath("\test.mdb")& ";"
Conn.Open
SQLTemp = "SELECT somecolumn FROM sometable"
set rstemp=Conn.execute(SQLTemp)
%>
<table><td>
<%
Do While Not rstemp.EOF
If (oddsevens MOD 2 = 0) Then
%>
<tr bgcolor=black><font color=white>
<%= rstemp.Fields("somedata").Value %>
</font>
<%
Else
%>
<tr bgcolor=white>
<%= rstemp.Fields("somedata").Value %>
<% End If %>
</tr>
<%
intNum = intNum + 1
rstemp.MoveNext
Loop
%>
</td></table>
<%
Conn.Close
Set Conn = Nothing
%> |
NOTE: Had trouble connecting to the database? Click here.






