by lgasoft
18. September 2009 05:46
Sometimes you are in the need of toggling a value in a loop.
This could for example be used for changing the css class in a list, a different class name for each item in the list.
So, how do you do this?
It’s really rather easy, let’s look at the following code:
Dim i ‘As Integer
i = 1
i = 1 – i
The result of the above calculation is of course 0.
So, now i equal 0
Let’s do it again:
i = 1 – i
And now i equal 1
Let’s do it again:
i = 1 – i
And suddenly i equal 0 again.
Now we have the logic clear to us, let’s try it in a real programming situation.
Create an .asp file and paste the following code into it, then put the .asp file in your web server and point your browser towards it.
<%@ Language="VBScript"%> <%Option Explicit%> <% With Response .Buffer=true .Expires=0 .Clear End With %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Toggle Value</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8""> <style type="text/css"> body { background-color: #FFFFFF; font-family: Verdana, Geneva, Arial, Tahoma, sans-serif; font-size: 80%; } div { border: solid 1px #505050; margin-bottom: 3px; padding: 4px; width: 200px; } .div0 { background-color: #E3E3E3; } .div1 { background-color: #F4F4F4; } </style> </head> <body> <% Dim i Dim y i = 0 For y = 1 To 12 Response.Write("<div class=""div" & i & """>" _ & "Div:" & y & " [i = " & i & "]</div>" & VbCrLf) i = 1 - i Next %> </body> </html> |
If all went well you should see something like this in your browser.

Download sample code: toggle.zip (650.00 bytes)