Writing the ASP code in JavaScript
To make colors alternate in a repeated region, you need to control the color attribute inside the tag that is repeatedtypically an HTML table row tag (<tr> ) or an HTML table tag (<table> ). For simplicity, this article assumes you applied the repeated region to a table row. Except for the HTML tag used, the information is the same for HTML tables.
The color attribute of the <tr> tag is bgcolor for example, <tr bgcolor="yellow"> . Your code must tell the application server to insert the bgcolor attribute in the <tr> tag every other time the tag is repeated.
Switch to Code view (View > Code) and find the beginning of the repeat region server behavior. To help you recognize the server behavior in the code, here's how UltraDev 4 starts an ASP repeated region using JavaScript:
<% while ((Repeat1__numRows-- != 0) && (!rsStaff.EOF)) { %>
Once you've located the start of the repeated region, declare and initialize a record counter variable somewhere above the repeated region, as follows:
<%
var RecordCounter = 0;
%>
The record counter keeps track of the rows in the recordset as the repeated region loops through each record.
Next, enter the following script inside the first <tr> tag after the beginning of the repeated region:
<tr
<%
RecordCounter++;
if (RecordCounter % 2 == 1)
Response.Write("bgcolor=silver");
%>
>
The script starts by incrementing the record counter by 1. The expression RecordCounter % 2 reads the record counter's value, divides it by 2, and returns the remainder. If the remainder is equal to 1 (that is, if the row number is an odd number), then the application server inserts the string bgcolor=silver inside the <tr> tag. Otherwise, it does nothing.
The previous code creates a repeated region with a single added color: the application server inserts a bgcolor attribute in one <tr> tag, leaves out the bgcolor attribute in the following <tr> tag, and so on. To create a repeated region with two alternating colors instead of one, enter the following code:
<tr bgcolor=
<%
RecordCounter++;
if (RecordCounter % 2 == 1)
Response.Write("silver");
else
Response.Write("#FFCC66");
%>
>
In this code, the attribute's name is hard-coded in the HTML (<tr bgcolor= ), meaning each row gets the attribute. The application server supplies the attribute's valuehere, silver or #FFCC66, a shade of orange.
|