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:
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:
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):
Along the same lines you can access them by index:
And perhaps the easiest way is to just get them
as a comma delimited list:
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:
Checkboxes are probably one of the two most confusing
HTML form elements. Dealing with them via ASP isn't hard,
but it can get confusing. Hopefully this will help clear
things up for you.
Note that I've used "Get" for the form so that you can see the
form data being transferred on the QueryString. I thought that
it might help you keep track of what's actually being sent back
to the server and understand the checkbox operation a little
Because of their similarity, if you haven't already seen it, you
should probably check out our radio button sample