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 Radio Button
By default, an HTML radio button element returns nothing
if it wasn't enabled and a value of "on" if it
was. So by checking its value we can determine if it was
enabled or not when the form was submitted.
The No-Frills Radio Button was:
Disabled
.
Radio Button With An Associated Value
If you don't like the fact that the radio button 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 radio button to be.
The Radio Button With An Associated Value returned:
.
Multiple Radio Buttons With The Same Name
Similarly to the checkbox, the scenario where you have a set
of radio buttons all with the same name is probably one of the
most useful ways to use this type of input tag. The main
difference is that instead of allowing a user to specify
multiple answers to one question, the radio button element
ensures that one and only one selection is choosen from among
the group of buttons that share a common name.
Multiple Radio Buttons With The Same Name ensured that only one
choice was selected and it was:
.
Radio Button Which Maintains Its State
At this point there's really nothing different about
this radio button 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-select all the appropriate
radio buttons.
In terms of the value, like I said, this is just like the
standard radio button, but just to keep up the pattern...
Radio Button Which Maintains Its State returned:
.
Radio Buttons 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 radio button operation a little
better.
Also note that while checkboxes allow you to deselect them if you
change your mind, radio buttons don't. Once
enabled, the only way to disable one is to select another radio button
on the form that has the same name.
Because of their similarity, if you haven't already seen it, you
should probably check out our checkbox sample
as well.