ASP.NET Web Form Validation Example

   

Now it's finally time to compare the traditional ASP version to my ASP.NET version of the same page. If you remember, I decided to develop two pages, one in traditional ASP and one in ASP.NET. Just for a refresher, the traditional ASP page took me 2 hours and 15 minutes to code and contained 168 lines of code.

I develop an ASP.NET page with similar function and it contained are you ready? 35 lines of code and took me 34 minutes to program. Just 35 lines of code. And it took less than 25% of the time to develop. Granted, this will vary depending on what your core skills are, but you can see how ASP.NET will massively reduce the amount of coding you need to do and the time it takes you to code.

The following is the code for the ASP.NET page. It may look like more than 35 lines, but this is only because I needed to format it in a way that it would display correctly in the book's format. And still, take notice that even though every property of every validation object gets a single line to itself in the example, it is still only 95 lines a whopping 73 lines shorter than the traditional example.

Validation ASP.NET Style aspx_validation.aspx
<%@ page language="vb" runat="server"%>  <script runat=server>  sub Page_Load(sender as Object, e as EventARgs)  if (IsPostBack) then  Validate()  if (IsValid) then  OurLabel.Text = "CONGRADULATIONS!!! The form is valid."  end if  end if  end sub  </script>  <html>  <head  <title></title>  </head>  <body bgcolor="#FFFFFF" text="#000000">  <form  runat="server" >  <asp:Label  runat="server" /><br>  <asp:ValidationSummary   Runat="server"  ShowMessageBox="true"  HeaderText="The following errors were found:"  DisplayMode="BulletList" />  Name:<asp:TextBox  runat="server" />  <asp:RequiredFieldValidator    ControlToValidate="Name"  Text="*"  ErrorMessage="Name is a Required Field"  runat="server" /> <br>  Address: <asp:TextBox  runat="server" />  <asp:RequiredFieldValidator    ControlToValidate="Address"  Text="*"  ErrorMessage="Address is a Required Field"  runat="server" /><br>  City: <asp:TextBox  runat="server" />  <asp:RequiredFieldValidator    ControlToValidate="City"  Text="*"  ErrorMessage="City is a Required Field"  runat="server" /><br>  State: <asp:TextBox  runat="server" />  <asp:RequiredFieldValidator    ControlToValidate="State"  Text="*"  ErrorMessage="State is a Required Field"  runat="server" /><br>  Zip: <asp:TextBox  runat="server" />  <asp:RequiredFieldValidator    ControlToValidate="Zip"  Text="*"  ErrorMessage="Zip is a Required Field"  runat="server" />  <asp:RegularExpressionValidator    ControlToValidate="Zip"  Text="*"  ErrorMessage="Enter a Valid Zip Code"  ValidationExpression= "^([0-9]{5}(( |-)[0-9]{4})?)"    runat="server" /><br>  Phone: <asp:TextBox  runat="server" /> Format: xxx-xxx-xxxx  <asp:RequiredFieldValidator    ControlToValidate="Phone"  Text="*"  ErrorMessage="Phone is a Required Field"  runat="server" />  <asp:RegularExpressionValidator    ControlToValidate="Phone"  Text="*"  ErrorMessage="Enter a Valid Phone Number"  ValidationExpression= "[0-9]{3}-[0-9]{3}-[0-9]{4}"  runat="server" /><br>  Email:<asp:TextBox  runat="server" />  <asp:RequiredFieldValidator    ControlToValidate="Email"  Text="*"  ErrorMessage="Email is a Required Field"  runat="server" />  <asp:RegularExpressionValidator    ControlToValidate="Email"  Text="*"  ErrorMessage="Enter a Valid Email"  ValidationExpression= "^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$"  runat="server" /><br>  <asp:Button  runat="server" text="Validate Form" /><br>  </form>  </body>  </html> 

That rocks!!! This is just one example of how ASP.NET will enable you to be more productive, write less code, and maybe go home for dinner like a normal person at a normal hour occasionally. (I know this will never happen because there will always be more code to write, but it's nice to dream.)


   
Top


ASP. NET for Web Designers
ASP.NET for Web Designers
ISBN: 073571262X
EAN: 2147483647
Year: 2005
Pages: 94
Authors: Peter Ladka

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net