<% '******************************************************* '* ASP 101 Sample Code - http://www.asp101.com/ * '* * '* This code is made available as a service to our * '* visitors and is provided strictly for the * '* purpose of illustration. * '* * '* http://www.asp101.com/samples/license.asp * '* * '* Please direct all inquiries to webmaster@asp101.com * '******************************************************* %>

Some sample checkbox form elements:

" method="get"> A Default No-Frills Checkbox:
Checkbox With An Associated Value:
Multiple Checkboxs With The Same Name:
Checkbox Which Maintains It's State: />

Let's check out our values just submitted:

FYI: Note that the above form submits back to this same page. If you haven't yet submit the form then there won't be any values in the text below since they are retrieved from the form. In this case, the values reflected are simply the defaults and are the same results you would get if you submitted the form without checking anything.

The Default No-Frills Checkbox

By default, an HTML checkbox element returns nothing if it wasn't checked and a value of "on" if it was. So by checking it's value we can determine if it was checked or not when the form was submitted. The No-Frills Checkbox was: <% ' Note that "on" is in lower case. Since writing "on" ' doesn't do much good, I simply check for it to determine ' which branch of the conditional to execute. I could ' place whatever code I wanted inside either condition. ' In this case I simply print out a "user-friendly" message. If Request.QueryString("default") = "on" Then Response.Write "Checked" Else Response.Write "Not Checked" End If %> .

Checkbox With An Associated Value

If you don't like the fact that the checkbox returns the value of "on" it's really easy to change. Simply add a value parameter and assign to it what you'd like the value returned by the checkbox to be. The Checkbox With An Associated Value returned: <%= Request.QueryString("value_specified") %>.

Multiple Checkboxs With The Same Name

Probably one of the most useful uses of the checkbox is to allow users to check multiple selections. Instead of having to chech each box's status individually you can simply name them all the same and the results will be returned in a convenient collection like fashion.

You can access them in a number of ways. First you can use the "For Each" syntax illustrated here (in the source code at least): <% Dim Item For Each Item In Request.QueryString("multiple") ' Do Whatever you want to do with each one. ' I'll just write them out with spaces after ' each one so they don't run together. Response.Write Item & " " Next %> .

Along the same lines you can access them by index: <% Dim I For I = 1 To Request.QueryString("multiple").Count ' Again I'm just writing them out. Response.Write Request.QueryString("multiple")(I) & " " Next %> .

And perhaps the easiest way is to just get them as a comma delimited list: <%= Request.QueryString("multiple") %> .

Checkbox Which Maintains It's State

At this point there's really nothing different about this checkbox from any other, but I thought I should illustrate the technique when building the form (see the form generating section of the source code) because it's so useful in real life. In particular, when you're sending a user back to the form to check their entries, it's really frustrating for them if they have to re-enter all their values and re-check all the boxes.

In terms of the value, like I said, this is just like the standard checkbox, but just to keep up the pattern... Checkbox Which Maintains It's State returned: <%= Request.QueryString("state_keeper") %>.

<% ' Here's a little function that you might find handy. ' It takes the name parameter of the checkbox you want ' to check and will return a boolean indicating if a ' checkbox with that name was checked on the form when ' the user submitted it even if the value of the ' checkbox isn't set to anything. It works well to ' help simplify your code... especially in conditionals. Function IsChecked(strFieldName) Dim blnChecked If Request.QueryString(strFieldName).Count > 0 Then blnChecked = True Else blnChecked = False End If IsChecked = blnChecked End Function ' For example, using the state_keeper example above: 'If IsChecked("state_keeper") Then ' ' The checkbox was checked. 'End If %>