ASP.NET : Tips, Tutorials and Code by Scott Mitchell, Stephen Walther, Donny Mack, Doug Seven |, Multimedia Set | Barnes & Noble
ASP.NET : Tips, Tutorials and Code

ASP.NET : Tips, Tutorials and Code

by Scott Mitchell, Stephen Walther, Donny Mack, Doug Seven

With a foreword by Microsoft's Mark Anders, Professional ASP.NET consists of 19 chapters written by seven of today's leading experts in ASP.NET. These authors are professional developers who create ASP.NET applications, teach, run well-known ASP.NET Web sites, either within or outside of Microsoft. The tutorial framework for each chapter includes:

  • A


With a foreword by Microsoft's Mark Anders, Professional ASP.NET consists of 19 chapters written by seven of today's leading experts in ASP.NET. These authors are professional developers who create ASP.NET applications, teach, run well-known ASP.NET Web sites, either within or outside of Microsoft. The tutorial framework for each chapter includes:

  • A brief introduction, explaining the concept.
  • A code example, illustrating the concept.
  • A piece-by-piece explanation of the code.

Most examples employ VB.NET, but there are also additional C# examples within each chapter, and all of the example programs will be available at the book's web site in both VB.NET and C#.

The code examples in this book are based upon the ASP.NET Beta2 specifications, a functionally complete version of the software.

Editorial Reviews
The Barnes & Noble Review
With ASP, Microsoft delivered an easy, powerful solution for creating dynamic web applications and pages -- and developers responded enthusiastically. Now, Microsoft is reinventing ASP as part of its .NET framework -- and, as Scott Mitchell puts it, "writing ASP.NET pages is an entirely different experience." The transition is likely to be worth the pain. ASP.NET pages are compiled, supercharging performance; the ASP.NET data caching module helps you eliminate pesky data access bottlenecks; ADO+ is way easier to use; apps are easier to deploy; and on, and on.

In ASP.NET: Tips, Tutorials, and Code, Mitchell organizes a team of ASP experts to probe every nook and cranny of ASP.NET. If you're an experienced ASP developer, this book -- based on the feature-complete ASP.NET Beta 2 -- will give you a great headstart on your next-generation web development projects.

Mitchell's team covers everything from form field input validation to error handling, data presentation to XML support. You'll find chapters on debugging; deployment and configuration; security; performance optimization; and separating code from content. You'll also find plenty of code -- mostly VB.NET, some C#, all of it on the CD-ROM, along with the complete ASP.NET beta it's based on.(Bill Camarda)

--Bill Camarda is a consultant, writer, and web/multimedia content developer with nearly 20 years' experience in helping technology companies deploy and market advanced products and services. He served for nearly ten years as vice president of a New Jersey-based marketing company, where he supervised a wide range of graphics and web design projects. His 15 books include Special Edition Using Word 2000.

Product Details

Publication date:
Sams White Book Series
Product dimensions:
7.40(w) x 9.00(h) x 1.80(d)

Read an Excerpt

Chapter 3: Form Field Input Validation

In this chapter you will learn
  • How to use the RequiredFieldValidator control
  • How to use the CompareValidator control
  • How to use the RangeValidator control
  • How to use the RegularExpressionValidator control
  • How to use the CustomValidator control
  • How to use the ValidationSummary control
If you're reading this book, you've no doubt created HTML forms to collect information from the users visiting your Web site. For example, if you want to create a form for a user to send feedback to the Webmaster, you might create a form with two text boxes: one for the user's email address (so the Webmaster can reply to the user's feedback), and another for the user's comments.

Unfortunately, HTML controls provide no sort of inherent data validation. That is, when you create a text box with the following code, the user can enter any sort of value, from something valid (such as Mitchell@ASP.NET) to something nonsensical (such as 76!r.2):

Enter your Email Address:<input type="text" name=="txtEmail"> With classic ASP, the developer would have had to create some sort of form-validation routine to ensure that the email address text box contains a valid email address.

The process of creating form-validation routines for every form on a Web site is both tiresome and error prone. Because no form-validation library shipped with classic ASP, developers were on their own to create their form-validation functions.

Fortunately, with ASP.NET, form validation is a breeze. ASP.NET pages can include validation controls, which are Web controls used to ensure that the entered values in form fields meet developer-defined criteria. In this chapter we will look at how to use these incredibly useful validation controls.

1. Validating Form Field Input with ASP.NET's Validation Controls

To help with form validation, ASP.NET ships with five validation controls (and a ValidationSummary control). Over the next six sections, we'll examine these five controls and the ValidationSummary control, looking at how they can be used to provide extensive form validation with just a few lines of code!

Before we delve into the validation controls, let's take a step back and look at fundamentals of form validation. Form validation can take one of two forms:

  • Client-side form validation
  • Server-side form validation
In client-side form validation, the HTML page contains client-side JavaScript code that is exe-cuted on the client's Web browser. This code is usually executed when the user attempts to submit a form: If the form fields are valid, the form submits; otherwise, the user is usually shown some message regarding his invalid form information. The nice thing about client-side form validation is that it executes entirely on the client's machine and doesn't require a roundtrip to the Web server. It can be used, then, to quickly detect any validation errors and alert the user of such errors. Client-side form validation has its downsides, though. For starters, it requires that the visitor be able to execute client-side JavaScript code on his browser. Some older browsers don't have this feature, and some Web surfers explicitly turn off JavaScript support in their Web browsers.

Server-side form validation, on the other hand, takes place after the user submits the form but before any action is done on the submitted data. The main advantage of server-side form vali-dation is that it will work regardless of the user's browser or browser configuration. Additionally, clever visitors can easily bypass any sort of client-side form validation. With server-side form validation, however, it is impossible for users to circumvent the validation process altogether.

When developing forms in classic ASP, developers were highly encouraged to use both client-side and server-side form validation because both have advantages. In my experiences, I found that most classic ASP developers just used server-side form validation (if any form validation at all). Using just client-side form validation should be avoided unless your Web application will be used in an environment where you know none of the users to be malicious and you are certain that they are all using modern browsers with client-side scripting features enabled.

The ASP.NET validation controls always use server-side form validation. However, if the user's browser can support DHTML features, client-side form validation is automatically included as well!

You can explicitly specify that the validation controls should never use client-side form validation on a page-by-page basis. Simply set the ClientTarget property to Downlevel using the @Page directive at the top of your ASP.NET page:

<%@Page ClientTarget="Downlevel " %%>

This will cause all the validation controls on the Web page to not use client-side form validation, regardless of the visitor's browser. Unfortunately you cannot specify client-side form validation on a validation control–by–validation control basis.

Again, regardless of whether client-side form validation is used, server-side form validation will always be employed by the ASP.NET validation controls.

2. Using the RequiredFieldValidator Control

The RequiredFieldValidator control, as its name implies, is useful when you have a form field that the user must enter before she can successfully submit the form.

There are many situations in which a form field requires an entry before processing can continue. For example, many Web sites have a “members-only" area, where a visitor must supply his username and password to proceed to a particular area of the Web site. With such a login form, both the username and password fields would be required fields. Listing 3.2.1 contains the code for an ASP.NET page that, when first loaded, presents the user with two text boxes for her username and password.

Listing 3.2.1 Using a RequiredFieldValidator Control Ensures That the User Enters a Value for a Form Field

1:<script language="vb " runat=="server ">
2:Sub Page_Load(sender as Object,e as EventArgs)
3:If Page.IsPostBack then
4:‘ The User has submitted the form..Is it valid?
5:If Page.IsValid then
6:‘ Yes it is,,check to make sure password/username are valid
7:‘ Then do whatever you intended on doing if they're login
8:‘ credentials were ((or were not)valid.
10:‘ The page is not valid,,meaning they either didn't enter
11:‘ the username or password
12:Response.Write(“You must enter both the username and password.")
13:End If
14:End If
15:End Sub
20:<form method="post " runat=="server ">
22:<asp:textbox runat="server " id=="txtUserName " //>
23:<!--Create a RequiredFieldValidator Control for txtUserName -->
24:<asp:RequiredFieldValidator runat="server "
25:id="reqUserName " ControlToValidate=="txtUserName "
26:Display="Dynamic ">
27:Enter your UserName!
31:<asp:textbox runat="server " id=="txtPassword " TextMode=="Password " //>
32:<!--Create a RequiredFieldValidator Control for txtPassword -->
33:<asp:RequiredFieldValidator runat="server "
34:id="reqPassword " ControlToValidate=="txtPassword "
35:Display="Dynamic ">
36:Enter your Password!
39:<p><asp:button runat="server " id=="btnSubmit " Text=="Login!" //>

Let's begin our examination of Listing 3.2.1 at line 18, where the HTML section begins. On line 20, a postback form is created. On line 22, a text box, txtUserName, is created for the visi-tor to enter her username into. Next, a RequiredFieldValidator control is created (lines 24 through 26)....

Meet the Author

Lead author Scott Mitchell is the editor and founder of, one of the largest ASP resource sites on the Web. He has been avidly using and writing about Active Server Pages since January 1998: he's written several hundred ASP-related articles on 4Guys along with authoring numerous beginner-level and advanced level books on Active Server Pages. He is the author of two well-regarded and widely read books on Active Server Pages: Teach Yourself Active Server Pages 3.0 in 21 Days and Designing Active Server Pages. Scott is a passionate developer whose love for ASP and desire to help others are well known among the ASP community.

Stephen Walther is a cofounder of Superexpert. Superexpert offers ASP.NET training to companies and individuals (see, produces ASP.NET controls (see, and offers a community Web site on ASP.NET and other Microsoft Technologies (see He is also very involved with the Seattle ASP.NET Users Group (see He is the author of three books on Active Server Pages: ASP.NET Unleashed, Active Server Pages Unleashed, and Teach Yourself E-Commerce with ASP in 21 Days.

Doug Seven, born and raised in Seattle, WA, is one of the co-founders of the .NET training company, Doug comes to by way of technical roles at Nordstrom, Microsoft and, and as a Training Specialist at Seattle Coffee Company. As the son of a programmer, Doug was destined to become a developer. Growing up around computers, his career began at the tender age of 11 writing "Choose Your Own Adventure" games in BASIC on a TRS-80, (unfortunately none of his groundbreaking work from this period survived). His first professional IT job was as a lead for the long-term data storage and disaster recovery group in a dual-IBM Mainframe environment at Nordstrom. After a seven-year sojourn as a rock star (seriously!), Doug took on the role of Technical Lead in Microsoft's Developer Support group. After leaving Microsoft, Doug worked as a developer in digital certificate enablement technology for before leaving to pursue life running his own company. provides hands-on training in ASP.NET and ADO.NET data access.

Donny Mack, native of Washington State is one of the co-founders of, the training company solely dedicated to ASP.NET and ADO.NET. is also a free online centralized resource website used by .NET developers to feed their .NET passion. Prior to founding with Doug Seven, Mack worked at Microsoft Corporation as a Visual InterDev/ASP Support Professional. Mack's need for bleeding edge technology is such that he spends his waking hours, (and some of his non-waking hours), writing code and developing content for Mack's real enjoyment comes from watching attendants of's training courses walk away as obsessed as he is with .NET.

Chris Payne has had a passion for computers and writing since a young age. He holds a Bachelor of Science in Biomedical Engineering from Boston University and supported himself through college as an independent consultant and writer of technical articles, focused on Web development. Currently making his home in Orlando, Florida with his fiancée, he works as a Web developer and is continuing his career as an author, both of technical and fictional material.

Billy Anders is a technologist whose zest for the dynamic can be seen in his contributions, as a Senior Consultant in the Advanced Technology Group of Microsoft Consulting Services in Redmond, WA. Over the past several years, Billy has focused on applications based on Microsoft technologies for both retail industry and enterprise customers. Billy is committed to teaching, as well as learning exciting, innovative web application programming technologies. While this is his first book project, he has lectured and written numerous articles on a variety of subjects including IIS, SQL Server, ASP and ASP.NET. When not exercising his mind, he exercises his spirit by motorcycling, snowboarding, and countless hours in the gym.

Adam Nathan is a Software Design Engineer on the Microsoft .NET Developer Platform QA team. Adam's focus is the Common Language Runtime-the core of the .NET platform—and its interoperability with COM. For the past two years, he has witnessed and influenced the design decisions that have shaped the product, and thus is able to give a unique perspective when explaining this technology to the reader. Adam has seen the areas of .NET with which programmers frequently struggle, and is armed with plenty of tips and tricks to share.

Dan Wahlin is an independent consultant for Wahlin Consulting and also founded

Customer Reviews

Average Review:

Write a Review

and post it to your social network


Most Helpful Customer Reviews

See all customer reviews >