Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alternate Table Row Colours in ASP 1

Status
Not open for further replies.

sila

Technical User
Aug 8, 2003
76
GB
Can anyone tell me how to code asp so that a list output into a table has alternating row colours?

Cheers
 
This has been coveed umpteen times in the past, te search tab is available above for more hits, but here are a few:
thread333-91042
thread333-336824
thread333-620665
thread333-389417

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
here you go just change the color code as needed
put inside <tr> tag


<%if intRowColor = 1 Then
Response.Write "<TR bgcolor=""#FFFF99"">"
intRowColor = 1
Else
Response.Write "<TR bgcolor=""#99CCFF"">"
intRowColor = 0
End if
%>

If knowledge were power I would be a AAA battery!
 
You're better off using either the "mod" method (from many of the links) or the one I tend to use:
Code:
'outside the loop
Dim bNeedsColor
bNeedsColor = True
Code:
'inside the loop
If bNeedsColor Then
    bgcolor = "#FFFFCC"
Else
    bgcolor = "#FFFFFF"
End If
bNeedsColor = Not bNeedsColor
String comparisons take too much time and needlessly burn cpu cycles. Server load, man, server load!
 
That last remark was the trigger for a test between the various ways to handle this.

Method 1: INT(i/2)
Code:
<style>
.even{	color: black;	background-color: white;}
.oneven{    COLOR: white;    BACKGROUND-COLOR: black;}
</style>
<h1>INT(i/2)=i/2</h1>
<%
dim start
start = timer
response.Write "<table>"
dim i
for i = 1 to 100000
 if i/2 = int(i/2) then
  response.Write "<tr class=even>"
 else
  response.Write "<tr class=oneven>"
 end if 
 response.Write "<td>test</td></tr>"
next
response.Write "<table>"

response.Write "Time: " & timer - start 

%>

You need that crazy number in the FOR..NEXT, otherwise you won't see differences. This BTW also makes clear that the real life difference in loading 1 page per method will be very small, because you normally do not present 100+ records.

Time method 1: 36 seconds



METHOD 2: STRING COMPARISON
Code:
<style>
.even{	color: black;	background-color: white;}
.oneven{    COLOR: white;    BACKGROUND-COLOR: black;}
</style>
<h1>STRING COMPARISON</h1>
<%
dim start
start = timer
response.Write "<table>"
dim c
c = "even"
for i = 1 to 100000
 if c = "oneven" then
  response.Write "<tr class=even>"
  c = "even"
 else
  response.Write "<tr class=oneven>"
  c = "oneven"
 end if 
 response.Write "<td>test</td></tr>"
next
response.Write "<table>"
response.Write "Time: " & timer - start 
%>

Time method 2: 26 seconds




METHOD 3: MOD
Code:
<style>
.even{color: black;	background-color: white;}
.oneven{COLOR: white;    BACKGROUND-COLOR: black;}
</style>
<h1>i MOD 2</h1>
<%
dim start
start = timer
response.Write "<table>"
dim i
for i = 1 to 100000
 if (i mod 2) = 0 then
  response.Write "<tr class=even>"
 else
  response.Write "<tr class=oneven>"
 end if 
 response.Write "<td>test</td></tr>"
next
response.Write "<table>"
response.Write "Time: " & timer - start 
%>

Time methode 3: 23 secods



METHOD 4: B = NOT B
Code:
<style>
.even{	color: black;	background-color: white;}
.oneven{    COLOR: white;    BACKGROUND-COLOR: black;}
</style>
<h1>B = NOT B</h1>
<%
dim start
start = timer
response.Write "<table>"
dim b
b = true
for i = 1 to 100000
 if b then
  response.Write "<tr class=even>"
 else
  response.Write "<tr class=oneven>"
 end if 
 b = not b
 response.Write "<td>test</td></tr>"
next
response.Write "<table>"
response.Write "Time: " & timer - start 
%>
Time method 4: 21 seconds

ttmug.gif
 
Errr...I'm getting differant results foxbox....I'll post back in a moment, wan to run anothe test, this one is running to close to the millisecond rounding

I put all the tests in one file and have them run one after another. They are currentl running 4 runs each of 100000 loops, giving me aveages, lows, and highs. Currently the order looks more like:
Boolean, Mod+string (tie), int

I have two more tests in there also, but my times are coming in at much lower than yours despite having copied your code and only altered it in a minor fashion (moved timers to functions, changed loop number to variable).

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
[highlight]Test descriptions[/highlight]
Method 1: If Int(i/2) = 1/2 Then (same as above)
Method 2: String Comparison (same as above)
Method 3: String comparison with concatenation **new
Method 4: If i Mod 2 = 0 Then (same as above)
Method 5: Mod concatenated for class name **new
Method 6: If B = Not B Then (same as above)

[highlight]Benchmark Results [/highlight]
Benchmark Results
Method Name Average Low High
Method #1 INT(i/2)=i/2 1.480 1.469 1.500
Method #2 STRING COMPARISON 1.020 0.984 1.094
Method #3 STRING COMPARISON W/ Concatenation 1.449 1.422 1.500
Method #4 i MOD 2 0.988 0.953 1.047
Method #5 i MOD 2 With class name concatenation 1.469 1.422 1.594
Method #6 B = Not B 0.871 0.859 0.891

Times are averages from 4 runs of 500000 loops for each method.

[highlight]Second group without test names in an attmpt to display results in readable fashion[/highlight]
Benchmark Results
Method Average Low High
Method #1 1.480 1.469 1.500
Method #2 1.020 0.984 1.094
Method #3 1.449 1.422 1.500
Method #4 0.988 0.953 1.047
Method #5 1.469 1.422 1.594
Method #6 0.871 0.859 0.891

Times are averages from 4 runs of 500000 loops for each method.


[highlight]Results Explained[/highlight]
In this case the fastest method appeared to be the B = Not B method. The Int() method came up as the slowest. I'm not sure if the disparity between my results and FoxBox's results are OS related, IIS versioning related, or what.
Also of note, I had to increase the number of loops by a factor of 5 to begin to approach the timing on FoxBox's, so the hardware speed differences or loads may be a part ofthis as well (though I do have 54 processes running in the background).

[highlight]Here is my code[/highlight]
Code:
<%
'------------------------------------------------
' Alternating Row Benchmark
' Written March 07, 03 based on tests written by Foxbox (tek-Tips.com)
' Tarwn
'------------------------------------------------ 
'
' Method 1: If Int(i/2) = 1/2 Then
' Method 2: String Comparison
' Method 3: String comparison with concatenation
' Method 4: If i Mod 2 = 0 Then
' Method 5: Mod concatenated for class name
' Method 6: If B = Not B Then
'------------------------------------------------
' Contents will be Response.Cleared afterwards so 
'	we don't have to deal with the browser output.
' This should not make a difference in the timer
'	outcome.
Dim m_time(6)
Dim m_avg(6), m_low(6), m_high(6)
Dim m_title(6)
Dim s_time

	m_title(1) = "INT(i/2)=i/2"
	m_title(2) = "STRING COMPARISON"
	m_title(3) = "STRING COMPARISON W/ Concatenation"
	m_title(4) = "i MOD 2"
	m_title(5) = "i MOD 2 With class name concatenation"
	m_title(6) = "B = Not B"

'------------------ Workhorse -------------------
' Runs te test code and displays results
Dim num_tests, num_runs
num_tests = 500000
num_runs = 4


Dim run_counter, method_counter

'initialize low, high, total time values
For method_counter = 1 to 6
	m_time(method_counter) = 0
	m_low(method_counter) = 100000
	m_high(method_counter) = 0
Next

'run the tests
Dim temp_time
For run_counter = 1 to num_runs
	For method_counter = 1 to 6
		temp_time = Runtest(method_counter, num_tests)	'get test results
		If temp_time = -1 Then
			Response.Write "Error Occurred: mc=" & method_counter & " rc=" & run_counter
			Response.End
		End If

		m_time(method_counter) = m_time(method_counter) + temp_time	'add time to total time for this method
		If temp_time < m_low(method_counter) Then m_low(method_counter) = temp_time	'if this is lowest, substitute
		If temp_time > m_high(method_counter) Then m_high(method_counter) = temp_time	'if this is highest, substitute
		If run_counter = num_runs Then m_avg(method_counter) = m_time(method_counter)/num_runs	'if last run then calc average

		temp_time = -1	'insurance on next loop
	Next
Next

Response.Write "<table style=""display:inline;""><tr><th colspan=""3"">Benchmark Results</th></tr>"
Response.Write "<tr><th>Method</th><th>Name</th><th>Average</th><th>Low</th><th>High</th></tr>"
For method_counter = 1 to 6
	Response.Write "<tr><td>Method #" & method_counter & "</td><td>" & m_title(method_counter) & "</td><td>" & FormatNumber(m_avg(method_counter),3) & "</td><td>" & FormatNumber(m_low(method_counter),3) & "</td><td>" & FormatNumber(m_high(method_counter),3) & "</td></tr>"
Next
Response.Write "</table><br>"
Response.Write "Times are averages from " & num_runs & " runs of " & num_tests & " loops for each method."


'------------------ Test Functions --------------
' These are the functions to handle the actual 6 
' method tests
Function Method1(tCount)
	StartClock
	%>
	<style>
		.even{    color: black;    background-color: white;}
		.oneven{    COLOR: white;    BACKGROUND-COLOR: black;}
	</style>
	<h1>INT(i/2)=i/2</h1>
	<%
	response.Write "<table>"
	dim i
	for i = 1 to tCount
		if i/2 = int(i/2) then
			response.Write "<tr class=even>"
		else
			response.Write "<tr class=oneven>"
		end if 
		response.Write "<td>test</td></tr>"
	next
	response.Write "<table>"

	Method1 = StopClock
	Response.Clear
End Function

Function Method2(tCount)
	StartClock
	%>
	<style>
		.even{    color: black;    background-color: white;}
		.oneven{    COLOR: white;    BACKGROUND-COLOR: black;}
	</style>
	<h1>STRING COMPARISON</h1>
	<%
	response.Write "<table>"
	dim c
	c = "even"
	for i = 1 to tCount
		if c = "oneven" then
			response.Write "<tr class=even>"
			c = "even"
		else
			response.Write "<tr class=oneven>"
			c = "oneven"
		end if 
		response.Write "<td>test</td></tr>"
	next
	response.Write "<table>"

	Method2 = StopClock
	Response.Clear
End Function

Function Method3(tCount)
	StartClock
	%>
	<style>
		.even{    color: black;    background-color: white;}
		.oneven{    COLOR: white;    BACKGROUND-COLOR: black;}
	</style>
	<h1>STRING COMPARISON W/ Concatenation</h1>
	<%
	response.Write "<table>"
	dim c
	c = "even"
	for i = 1 to tCount
		Response.Write "<tr class=" & c & ">"
		if c = "oneven" then c = "even" else c = "oneven"
		response.Write "<td>test</td></tr>"
	next
	response.Write "<table>"

	Method3 = StopClock
	Response.Clear
End Function

Function Method4(tCount)
	StartClock
	%>
	<style>
	.even{color: black;    background-color: white;}
	.oneven{COLOR: white;    BACKGROUND-COLOR: black;}
	</style>
	<h1>i MOD 2</h1>
	<%
	response.Write "<table>"
	dim i
	for i = 1 to tCount
		if (i mod 2) = 0 then
			response.Write "<tr class=even>"
		else
			response.Write "<tr class=oneven>"
		end if 
		response.Write "<td>test</td></tr>"
	next
	response.Write "<table>"

	Method4 = StopClock
	Response.Clear
End Function


Function Method5(tCount)
	StartClock
	%>
	<style>
	.row_0{color: black;    background-color: white;}
	.row_1{COLOR: white;    BACKGROUND-COLOR: black;}
	</style>
	<h1>i MOD 2 as style class</h1>
	<%
	response.Write "<table>"
	dim i
	for i = 1 to tCount
		Response.Write "<tr class=row_" & i mod 2 & ">"
		response.Write "<td>test</td></tr>"
	next
	response.Write "<table>"

	Method5 = StopClock
	Response.Clear
End Function

Function Method6(tCount)
	StartClock
	%>
	<style>
	.even{    color: black;    background-color: white;}
	.oneven{    COLOR: white;    BACKGROUND-COLOR: black;}
	</style>
	<h1>B = NOT B</h1>
	<%
	response.Write "<table>"
	dim b
	b = true
	for i = 1 to tCount
		if b then
			response.Write "<tr class=even>"
		else
			response.Write "<tr class=oneven>"
		end if 
		b = not b
		response.Write "<td>test</td></tr>"
	next
	response.Write "<table>"

	Method6 = StopClock
	Response.Clear
End Function

'------------------ Clock Functions -------------
' Usually these would be in a language that has
' better millisecond resolution, but shouldn't
' be necessary in this case
Function StartClock()
	s_time = timer
End Function

Function StopClock()
	StopClock = timer - s_time
End Function

'------------------ RunTest Function ------------
' Allows us to make one call and run any test
Function RunTest(testNum, loopCount)
	Select Case testNum
		Case "1"
			Runtest = Method1(loopCount)
		Case "2"
			Runtest = Method2(loopCount)
		Case "3"
			Runtest = Method3(loopCount)
		Case "4"
			Runtest = Method4(loopCount)
		Case "5"
			Runtest = Method5(loopCount)
		Case "6"
			Runtest = Method6(loopCount)
		Case Else
			Runtest = -1
	End Select
End Function
%>

Not sure how the formatting on the results will turn out.


01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Whoops, I juts noticed that those numbers he was posting was test : time...i thought they were seconds : ms, my bad. I guess the results do agree, though the disparity in time still bothers me...

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
First: in my view both tests seem to agree on the speed order: boolean, mod, string, int.

The int() method is slow because each time 2 divisions and 1 comparison are needed. With MOD only 1 division is needed which apparently is slower than an assignment (b= not b) and a true/false IF.

Its my belief that the main difference in overall time lies in the actual response.writing i did in my test (i'm too lazy to test that now, because at the moment i'm having fun with building a RSS option).

Here are my results, running The Tarwn Test:

Benchmark Results
Method Name Average Low High
Method #1 INT(i/2)=i/2 2.012 2.000 2.016
Method #2 STRING COMPARISON 1.285 1.281 1.297
Method #3 STRING COMPARISON W/ Concatenation 1.832 1.828 1.844
Method #4 i MOD 2 1.246 1.234 1.250
Method #5 i MOD 2 With class name concatenation 1.813 1.813 1.813
Method #6 B = Not B 1.109 1.109 1.109

Times are averages from 4 runs of 500000 loops for each method.
-----
So you have better hardware; which is no surprise to me, because i did my test on 2 local W2K PII-desktops (with nothing in the background [tongue]).

ttmug.gif
 
why is it always me with the slowest machine

Method Name Average Low High
Method #1 INT(i/2)=i/2 2.634 2.594 2.715
Method #2 STRING COMPARISON 1.624 1.590 1.664
Method #3 STRING COMPARISON W/ Concatenation 2.448 2.391 2.586
Method #4 i MOD 2 1.695 1.672 1.742
Method #5 i MOD 2 With class name concatenation 2.400 2.383 2.422
Method #6 B = Not B 1.364 1.352 1.383

Times are averages from 4 runs of 500000 loops for each method.


Great to see some thinking going again.


___________________________________________________________________

The answer to your ??'s may be closer then you think.
Check out Tek-Tips knowledge bank by clicking the FAQ link at the top of the page
 
Boolean rules!

I knew my "intuitive reasoning" would pay off without empirical evidence. Flipping a bit is so much simpler than any kind of integer arithmetic and can't help but be simpler than string comparison.

Good to feel vindicated. :)
 
Yep, and it looks like I'll have to stop doing the Mod concatenation thing, string concatenation must be more expensive then an if ceck (although, when I say it that simply, it makes sense :p)

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top