ASP 101 - Active Server Pages 101 - Web04
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash





ASP Source Code:
<%
' Declare variables:
Dim blnFormIsValid
Dim strName, strGender, strColor, blnAgree

' Read in the data entered into the form:
strName   = Request.Form("txtName")
strGender = Request.Form("radGender")
strColor  = Request.Form("ddlColor")
blnAgree  = CBool(Request.Form("chkAgree") = "on")

' Do data validation:
blnFormIsValid = True
If strName = ""   Then blnFormIsValid = False
If strGender = "" Then blnFormIsValid = False
If strColor = ""  Then blnFormIsValid = False
If Not blnAgree   Then blnFormIsValid = False

' Since I'm illustrating how to maintain the form's data,
' I'll force the form to fail validation so that the page
' will always display the form no matter what values are
' entered.  In order to actually use a form of this type
' you should remove or comment out the following line:
blnFormIsValid = False

If blnFormIsValid Then
    ' Do whatever processing you want to do when
    ' the form is completed:
    %>
    <p>
    Thank you for filling out the form.
    </p>
    <p>
    You entered:
    </p>
    <pre>
              <strong>Name:</strong> <%= strName %>
            <strong>Gender:</strong> <%= strGender %>
    <strong>Favorite Color:</strong> <%= strColor %>
    <strong>Agree to Terms:</strong> <%= blnAgree %>
    </pre>
    <%
Else
    ' Show the form:
    ' Take note of the different approach used to persist the form data
    ' for each type of form element.  For text boxes, we set the value
    ' attribute to the text we read in previously.  For each checkbox
    ' and radio button we need to determine if it should be "checked"
    ' or not.  The most annoying type is the select element (aka.
    ' Drop Down List).  Unfortunately we need to check each of the
    ' option elements in order to determine which one was "selected"
    ' and then mark it as such in the HTML we return to the client.
    %>
    <form action="<%= Request.ServerVariables("URL") %>" method="post">

    <table border="0">
        <tr>
            <td align="right"><strong>Name:</strong></td>
            <td>
                <input type="text" name="txtName" value="<%= strName %>" />
            </td>
        </tr>
        <tr>
            <td align="right"><strong>Gender:</strong></td>
            <td>
                <input type="radio" name="radGender" value="Male"
                <% If strGender = "Male" Then Response.Write " checked=""checked""" %> />Male
                <input type="radio" name="radGender" value="Female"
                <% If strGender = "Female" Then Response.Write " checked=""checked""" %> />Female
            </td>
        </tr>
        <tr>
            <td align="right"><strong>Favorite Color:</strong></td>
            <td>
                <select name="ddlColor">
                    <option<% If strColor = "Red"    Then Response.Write " selected=""selected""" %>>Red</option>
                    <option<% If strColor = "Orange" Then Response.Write " selected=""selected""" %>>Orange</option>
                    <option<% If strColor = "Yellow" Then Response.Write " selected=""selected""" %>>Yellow</option>
                    <option<% If strColor = "Green"  Then Response.Write " selected=""selected""" %>>Green</option>
                    <option<% If strColor = "Blue"   Then Response.Write " selected=""selected""" %>>Blue</option>
                    <option<% If strColor = "Indigo" Then Response.Write " selected=""selected""" %>>Indigo</option>
                    <option<% If strColor = "Violet" Then Response.Write " selected=""selected""" %>>Violet</option>
                </select>
            </td>
        </tr>
        <tr>
            <td align="right"><strong>Agree to Terms:</strong></td>
            <td>
                <input type="checkbox" name="chkAgree"
                <% If blnAgree Then Response.Write " checked=""checked""" %> />
            </td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" /></td>
        </tr>
    </table>

    </form>
    <%
End If
%>

<p>
<strong>Note:</strong> 
The point of this sample is to illustrate repopulating the form fields with
the values the user enters.  As such, regardless of the values entered,
the form will not actually do anything when submitted.  This is intentional.
To change this behavior, please read the comments included in the source code.
</p>

Back the the Sample Output

Back to the Sample Index


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers