Checkbox.asp

A simple Classic ASP checkbox implementation, helpful for beginners.
Originally from http://www.asp101.com

CodeFunctionName
What is this?

Public

Tested

Imported
<%
'*******************************************************
'* 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. *
'* *
'* Please direct all inquiries to webmaster@asp101.com *
'*******************************************************
% >

<h2 >Some sample checkbox form elements: </h2 >

<form action=" <%= Request.ServerVariables("URL") % >" method="get" >
A Default No-Frills Checkbox: <input type="checkbox" name="default" / >
<br / >
Checkbox With An Associated Value:
<input type="checkbox" name="value_specified" value="Some Value" / >
<br / >
Multiple Checkboxs With The Same Name:
<input type="checkbox" name="multiple" value="1" / >
<input type="checkbox" name="multiple" value="2" / >
<input type="checkbox" name="multiple" value="3" / >
<br / >
Checkbox Which Maintains It's State:
<input type="checkbox" name="state_keeper"
<%
' This is the code that keeps the state of the checkbox.
' All it does is check to see if the value returned is the same as the value parameter of the checkbox input tag. If so, the box was checked and we output the input tag's "checked" attribute to keep it that way.
If Request.QueryString("state_keeper") = "on" Then
Response.Write " checked=""checked"""
End If
% >
/ >
<br / >
<br / >
<input type="submit" / >
</form >
<br / >
<br / >
<br / >

<h2 >Let's check out our values just submitted: </h2 >
<blockquote > <em >
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 retreived 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.
</em > </blockquote >

<p > <strong >The Default No-Frills Checkbox </strong > </p >
<p >
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:
<strong >
<%
' 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
% >
</strong >.
</p >

<p > <strong >Checkbox With An Associated Value </strong > </p >
<p >
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:
<strong > <%= Request.QueryString("value_specified") % > </strong >.
</p >

<p > <strong >Multiple Checkboxs With The Same Name </strong > </p >
<p >
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.
</p >
<p >
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):
<strong >
<%
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
% >
</strong >.
</p >
<p >
Along the same lines you can access them by index:
<strong >
<%
Dim I
For I = 1 To Request.QueryString("multiple").Count
' Again I'm just writing them out.
Response.Write Request.QueryString("multiple")(I) & " "
Next
% >
</strong >.
</p >

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

<p > <strong >Checkbox Which Maintains It's State </strong > </p >
<p >
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.
</p >
<p >
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:
<strong > <%= Request.QueryString("state_keeper") % > </strong >.
</p >

<%
' 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


% >

Views 2,003

Downloads 720

CodeID
DB ID