ASP/.NET Web Hosting
* 3 Months FREE *
CLICK HERE!
 CoverYourASP --> Implementing forms with ASP --> Part 2

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
163 active users
10230 visitors today
11070 pages today
how is this done?
ASP.NET Showcase
ASP.NET Graph and Chart Component with FREE developer version

ASP.NET Shopping Cart and E-commerce Control with FREE trial download

ASP.NET Web Hosting with free month and setup

$15 Domain Name Registration from .DNR.
As seen on these sites
Special thanks go to the sites that support me by publishing my articles.
See more

CoverYourASP
Copyright © 1999-2008 James Shaw.
All rights reserved.

ASP.NET Blog
ASPRSS
ShawThing

Now open source with SourceForge!

The <form> framework

var bSubmitted = (Request.Form.Count > 0);
var sName = 'egghead';

// has the form been submitted?
if ( bSubmitted )
{
   // get the data from the form...
   sName = ''  + Request.Form ( "name" );

I start by testing the amount of data in the Form collection of the Request object. If there is some (if Count is greater than zero) then the form has been "submitted".

This means that the form has already been displayed, filled in by the user, the submit button pressed and the data sent to the page that was referenced by the <form> tag (I'll show you that later).

On the second line I declare and initialize all the variables that will contain the data on the form. You need one variable per input. They are initialized to the default value that you want on the form when it is first displayed.

If the form has been submitted I need to validate the data entered by the user. The code does the steps out of order like this so that I have a chance to re-display the form if invalid data is entered.

I set the variables to the data on the form by asking the Form collection for the values by the names I specified. Remember to always force the data to be the correct type by either adding a blank string, or subtracting zero as shown below:

var sName = ''  + Request.Form ( "name" );
var nNumber = Request.Form ( "age" ) - 0;

Then do the actual validation, and if it fails display a message and set bSubmitted=false to cause the form to be displayed again:

   // validate the name
   if ( !sName.length )
   {
      Out ( 'You must enter a name<p>' );

      // pretend the form hasn't been sent yet
      bSubmitted = false;
   }
}

Part 3: Displaying the form...

Featured sponsor
My favorite resources

Selling products online - read how James does it now



See my source code
wherever you see this icon...

You can also download the entire site source code for FREE!


New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!