asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

An Introduction to the ASP.NET 2.0 Wizard Control - Validation
By John Peterson
Rating: 3.4 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article



  • download source code
  • This article is a follow up to my article An Introduction to the ASP.NET 2.0 Wizard Control, which was published a few weeks ago. It seems that many of you are unsure how to use the ASP.NET validation controls in conjunction with the new ASP.NET 2.0 Wizard control. The process is largely the same as performing validation of any other form, but due to the large number of questions I've received, I figured it wouldn't hurt to do a quick walk through.

    I'll be using the two samples from the previous article as a starting point. If you want to follow along and don't already have the sample pages, you can download a zip file containing them both from here (2.7 KB).

    Once again, I'm going to be building my samples using Microsoft Visual Web Developer 2005 Express Edition. While you certainly don't need VWD 2005 in order to use the code from this article, if you are interested in getting a copy, it's available as a free download from the Visual Studio Express web site.

    Adding Validation to the Basic Wizard Sample

    1. Load Visual Web Developer and open the "Wizard" project you created last time. If you don't have one, simply create a new "ASP.NET Web Site" project, name it "Wizard" and copy the files from the zip file mentioned above into it.

    2. Since I'm going to be using the same two files we used last time, I'm going to create a new copy of each so that you can keep the originals for reference.

      Create a new copy of "wizard_basic.aspx" and name it "wizard_basic_validation.aspx". While you're at it you might as well do the advanced version as well. For that one, copy "wizard_advanced.aspx" to "wizard_advanced_validation.aspx".

      Your project should now look something like the image above. The two new files are the ones highlighted.

    3. Open the new file "wizard_basic_validation.aspx" in Design view. The design pane should look something like this:

    4. I'm now going to add a ValidationSummary control to the page directly above the existing Wizard Control. You can find the ValidationSummary control in the Validation section of the Visual Studio toolbox.

      Change the control name to "validSummary" and enter the following as the control's HeaderText property: "This WizardStep did not validate, please address the following issues:" The resulting page should look like this:

    5. The next step is to add the actual validation controls to the Wizard, associate them with the form fields they will be validating, and set any validation rules we need to in order to get them working the way we want them to.

      For the first Wizard Step, the only validation we're going to do is to be sure that the user enters something for their name. For this I'm going to use the RequiredFieldValidator control with you can find on the same toolbox tab as the ValidationSummary control.

      With the first Wizard Step showing, drag and drop a RequiredFieldValidator control onto the page directly to the right of the TextBox where the user should enter their name. You can play with the exact placement of the control later once we get everything working, but for now we need to set some of the control's properties. First off, change the control's ID to "validName" and set the ControlToValidate property to "txtName" (the name of our TextBox control). Next set the Text property to "*" and the ErrorMessage to "Please enter your name." Finally I set some optional properties that simply make the form look a little better and make it a little easier to use (Display = "Dynamic" and SetFocusOnError = "True")

      The page in design mode should now look like this:

    6. When viewed in a browser the page looks the same as it did before. The only time anything changes is when the user inputs data that doesn't pass our validation criteria -- in our case, leaves the name field blank. When that happens, the user gets a message that looks like the image below and the wizard won't go on to the next step until the user resolves the validation issue.

    7. Back in Visual Web Developer, it's time to finish adding validation to the page by adding both a RequiredFieldValidator control and a RegularExpressionValidator control to the e-mail TextBox in the second Wizard Step.

      First we need to switch to the seconds Wizard Step. Do that by clicking on the "Step 2" in the Wizard control.

      I'm going to start with the RequiredFieldValidator since it's almost identical to the one we just did to validate the Name TextBox.

      Once again we're going to drag and drop a RequiredFieldValidator control onto the page directly to the right of the TextBox. Once the validator is in place, select it and set the following properties to the corresponding values:

      Property Value
      ID"validEmailRequired"
      ControlToValidate"txtEmail"
      Text"*"
      ErrorMessage"Please enter your e-mail address."
      Display"Dynamic"
      SetFocusOnError"True"

      Then we're going to do the same with a RegularExpressionValidator control. Drop it right next to the control you just added and set its properties to the following:

      Property Value
      ID"validEmailRegEx"
      ControlToValidate"txtEmail"
      ValidationExpression"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
      Text"*"
      ErrorMessage"Please enter a valid e-mail address."
      Display"Dynamic"
      SetFocusOnError"True"

      And before I get all sorts of email -- I'm not a Regular Expression guru. The email validation above was generated by Visual Web Developer.

      Your page will now look something like this:

    8. When you're finished with the validators for Step 2, be sure to switch the wizard back to Step 1, save the file and give it a whirl.

    9. As before, if all entries pass validation the user won't see anything new. But, if they happen to enter an invalid email address, they'll be greeted by the following message asking them to please check their entries.

    Adding Validation to the Advanced Wizard Sample

    Having covered all the basics in the basic sample, I'm going to skip doing a step-by-step write up of what you need to do to add validation the advanced sample. That being said, rest assured, I did actually do it and for those of you too lazy to actually look in the zip file for the code, here's a quick screen shot to prove it.

    But seriously, the concepts are all the same and everything is just as simple. The only difference is that because the sample has more data to validate, we end up with a lot more validation controls.

    Conclusion

    I hope this has helped clear up any confusion and illustrate just how easy it is to use the ASP.NET validation controls in conjunction with the ASP.NET 2.0 Wizard control in order to create fabulous multi-page web forms that actually validate their data before passing it on to its final destination.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Jul 21, 2005 - N-Tier Web Applications using ASP.NET 2.0 and SQL Server 2005 - Part 1
    While the .NET Framework made building ASP.NET applications easier then it had ever been in the past, .NET 2.0 builds on that foundation in order to take things to the next level. This article shows you to how to construct an N-Tier ASP.NET 2.0 Web application by leveraging the new features of ASP.NET 2.0 and SQL Server 2005.
    [Read This Article]  [Top]
    Apr 28, 2005 - New Files and Folders in ASP.NET 2.0
    With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.NET by introducing a suite of new features and functionalities. As part of this release, ASP.NET 2.0 also comes with a host of new special files and folders that are meant to be used to implement a specific functionality. This article examines these new files and folders in detail and provides examples that demonstrate how to utilize them to create ASP.NET 2.0 applications.
    [Read This Article]  [Top]
    Mar 10, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2, Cont'd
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 9, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 3, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1, Cont'd
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Mar 2, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Feb 16, 2005 - Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
    In ASP.NET 2.0 and Visual Studio 2005, you can quickly program custom authentication pages with the provided Membership Login controls. In this article, Dina Fleet Berry examines the steps involved in using the Login control with a custom SQL Server membership database.
    [Read This Article]  [Top]
    Dec 29, 2004 - ClickOnce Deployment in .NET Framework 2.0
    In this article, Thiru Thangarathinam examines .NET 2.0's new ClickOnce deployment technology that is designed to ease deployment of Windows forms applications. This new technology not only provides an easy application installation mechanism, it also eases deployment of upgrades to existing applications.
    [Read This Article]  [Top]
    Dec 15, 2004 - A Sneak Peek at ASP.NET 2.0's Administrative Tools
    With ASP.NET 2.0, Microsoft has made great strides in increasing developer productivity and has made implementing previously complex solutions relatively easy. Where this version of ASP.NET really shines, however, is in its new administrative tools that allow developers to spend less time managing the configuration of the servers and software and more time developing great code.
    [Read This Article]  [Top]
    Nov 17, 2004 - The ASP.NET 2.0 TreeView Control
    Thiru Thangarathinam introduces ASP.NET 2.0's new TreeView control which provides a seamless way to consume and display information from hierarchical data sources. The article discusses this new control in depth and explains how to use this feature rich control in your ASP.NET applications.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry

    internet.commediabistro.comJusttechjobs.comGraphics.com

    Search:

    WebMediaBrands Corporate Info

    Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
    Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs