Friday, February 11, 2011

Form Validation

ASP.NET makes it really simple, maybe to simple, to add form validation to your web forms. Suppose we had a simple form that consisted of these fields:


  • First Name
  • Last Name
  • Email Address
The code for such a form would probably resemble this:

  1.  <form id="simpleForm" runat="server">
  2.     <div>
  3.    
  4.         First Name:<asp:TextBox ID="txtFName" runat="server"></asp:TextBox>
  5.         <br />
  6.         Last Name:<asp:TextBox ID="txtLName" runat="server"></asp:TextBox>
  7.         <br />
  8.         Email Address:<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
  9.         <br />
  10.        
  11.     </div>
  12.  </form>

After we add our validation controls, which you can add from the "Validation" tab of the Toolbox, the code looks like this:

  1.     <form id="signUp" runat="server">
  2.     <div style="width: 957px">
  3.    
  4.         First Name:<asp:TextBox ID="txtFName" runat="server"></asp:TextBox>
  5.         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
  6.             ControlToValidate="txtFName" ErrorMessage="First Name Required"></asp:RequiredFieldValidator>
  7.         <br />
  8.         Last Name:<asp:TextBox ID="txtLName" runat="server"></asp:TextBox>
  9.         <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
  10.             ControlToValidate="txtLName" ErrorMessage="Last Name Required"></asp:RequiredFieldValidator>
  11.         <br />
  12.         Email Address:<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
  13.         <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
  14.             ControlToValidate="txtEmail" ErrorMessage="Enter a vaild email address"
  15.             ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
  16.         <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
  17.             ControlToValidate="txtEmail" ErrorMessage="Email address required"></asp:RequiredFieldValidator>
  18.         <br />
  19.        
  20.     </div>
  21.     </form>

And what will happen now is our web form will make the First Name, Last Name and email fields required, as well as validate that the email address is really an email address.

Yield: Button redirection

Sometimes it is necessary to provide a way to get from one web page to another. Normally this is done with links, but sometimes you might come across a situation where you need to do this from within your code.

This can be accomplished in ASP.NET by using the "Response" property of the current web form. Keep in mind that the "Response" property is just a System.Web.HttpResponse(), and this means that anything we could do with the System.Web.HttpResponse(), we can do with the "Response" property of our web form.

Linking to an external web page using the "Response" property of the web form:
  1. Response.Redirect("http://ibreakcode.blogspot.com")

Linking to an internal web page using the "Response" property of the web form:
  1. Response.Redirect("Default2.aspx")

And thats about all there is to that.

Creating A Blank ASP.NET Project

Microsoft Visual Studio 2010 gives you a few options to create an ASP.NET website pre-loaded with a bunch of websites already built for you. This is great if you just want something to play around with something, but don't do you much good if you don't know whats going on.

For this reason, the remainder of the ASP.NET posts, unless otherwise noted will be using blank ASP.NET website projects.

To create a blank ASP.NET website project, open Microsoft Visual Studio and follow these steps:



  1. Click the "File" menu option on the main menu
  2. Click the "New" sub-menu option
  3. Click the "Web Site..."
After you click the menu option in Step 3 , the following dialog will appear (Note: I'm using Microsoft Visual Studio 2010 Professional):


On the dialog box, you will see something titled "Web location" and you should have the following options:
The "New Web Site" dialog.

  • File System
    • This creates a website on your local file system. Such as C:\Websites. These website are not available to the actual Internet via IIS or other server technologies, unless you create a virtual directory pointing to them. 
  • HTTP
    • These are IIS websites located on the local computer, or a remote computer.
  • FTP
    • These are websites that you connect to with FTP. You will need to provide credentials to access the FTP server.
More information on ASP.NET web locations can be found here.
    When selecting the project template, choose "ASP.NET Empty Web Site". This will create a blank ASP.NET project where you can add your own web site components.

    Microsoft has an example project that you can play around with if you choose "ASP.NET Web Site"
    I Break Code Where code gets done.
    ASP.NET | HTML | SQL Server | VB.NET | Request A Topic |