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

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
66 active users
245 visitors today
486 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!

Implementing forms with ASP

I think it's safe to say that every site needs a form - a contact or tell-a-friend form to send email, a Search form to search your database, or some sort of data entry form - probably all of these!

As you'll soon see, the framework for all these forms is the same. Once you've written one, the rest are easy - it's just copy and paste! The steps to follow are:

display form
      v
validate data
      v
process data
      v
[display form]

Below is the code that I use to implement this framework. Every form uses some variation of this - some don't need any validation, others always display the form again. But each form starts with the code below.

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" );

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

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

// show the form if not submitted yet
if ( !bSubmitted )
{
   Out ( 'Here\'s a simple form.' );

   Out ( '<form action="FormPage.asp" method="post">' );

      Out ( 'Name: ' );
      Out ( '<input type="text" name="name" size="10" value="' + sName + '">' );
      Out ( '<input type="submit" value="Display name">' );

   Out ( '</form>' );
}
else
{
   // process data
   Out ( 'Hi ' + sName );
}

Take a moment to glance over the code above, then I'll step you through this code, line-by-line, and examine what is happening.

Part 2: The framework...

Featured sponsor
My favorite resources

Selling products online - read how James does it now



I share my content

Supporting ASPRSS

Do you need a quick and easy way to link to my articles? All the information you need is published with ASPRSS...


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!