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
117 active users
338 visitors today
255 pages today
how is this done?
Tools I use

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

ASP.NET Blog
RSS submissions
E-commerce

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


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

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


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...