Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

HOW TO: Use the RequiredFieldValidator Control with Other Validation Controls to Handle Blank Entries


View products that this article applies to.

Summary

This step-by-step article describes how to use the RequiredFieldValidator control to handle blank entries when you use the other validation controls that are available with ASP.NET.

All validation controls except RequiredFieldValidator report a blank entry as valid input. If you want to prevent blank entries with validation controls, you must use a RequiredFieldValidator control. This adds to the overall flexibility of the validation controls, and allows you to provide a different error message to address blank entries.

Requirements

The following items describe the recommended hardware, software, network infrastructure, skills and knowledge, and service packs that you need:
  • Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 Advanced Server, or Microsoft Windows XP
  • Microsoft .NET Framework
  • Microsoft Internet Information Services (IIS)

Create a Sample ASP.NET Web Application

To create a new ASP.NET Web application named ValidatorSample, follow these steps:
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, click Visual C# Projects under Project Type, and then click ASP.NET Web Application under Templates.NOTE: If you want to create a Visual Basic ASP.NET Web Application project, select Visual Basic under Project Type. The rest of the steps in this article are the same for either project type.

  4. In the Location text box, replace WebApplication# in the URL path with ValidatorSample. If you are using the local server, you can leave the server name as http://localhost. If you do this, the Location text box entry is http://localhost/ValidatorSample.

Create the Validation Sample

NOTE: When you use several validation controls on a Web Form, you can use a Validation Summary control to display any error messages in a central location. In this way, you can give the page a much cleaner presentation.

To create a sample Web Form page that demonstrates how to use the RequiredFieldValidator control as a solution to handle blank entries when you use the other available validation controls, follow these steps:
  1. Add a new Web Form to the Validatorpage.aspx project. To do this, right-click the project node in Solution Explorer, click Add, and then click Add WebForm. Name the .aspx page Validatorpage.aspx, and then click Open.
  2. With the .aspx page open in Design view, drag two TextBox controls from the Web Forms section of the toolbar onto the page.
  3. Drag a Web Form Button control onto the page.
  4. Drag a CompareValidator control onto the page. Modify the properties of the control as follows:
    1. In the Properties pane, change ControlToValidate to TextBox2.
    2. Change ControlToCompare to TextBox1.
    3. Type Invalid Value for CompareValidator! for the ErrorMessage property.
  5. On the File menu, click Save All to save the Web Form and other associated project files.
  6. On the Build menu in the Visual Studio .NET integrated development environment (IDE), click Build to build the project.
  7. Right-click the Web Form page, and then click View In Browser.
  8. With the page displayed in the browser, type Hello in both TextBox1 and TextBox2, and then click Button1. Because this is considered valid input for TextBox2, no error message is displayed.
  9. Leave the word "Hello" in TextBox1, type Goodbye in TextBox2, and then click Button1. The CompareValidator control displays error message text to show that the value for TextBox2 is not valid when compared to TextBox1.
  10. Clear TextBox2, and then click Button1. Although you may expect to receive an error message, the blank entry is reported as valid.
  11. Switch back to Design view in the Visual Studio .NET editor. In the Web Forms section of the toolbox, drag a RequiredFieldValidator control onto the page. In the Properties pane, set the ControlToValidate property to TextBox2, and then type Entry Required! for the ErrorMessage property.
  12. On the File menu, click Save All to save the Web Form and other associated project files.
  13. On the Build menu in the Visual Studio .NET IDE, click Build to build the project.
  14. Right-click the Web Form page, and then click View In Browser.
  15. With the page displayed in the browser, type Hello in TextBox1 and leave TextBox2 blank. Click Button1. Because you have added the RequiredFieldValidator control, you receive the "Entry Required!" error message.

REFERENCES

For additional information about ASP.NET, click the article number below to view the article in the Microsoft Knowledge Base:
305140 INFO: ASP.NET Roadmap
For more information about ASP.NET server controls and a listing of additional resources, see the following Knowledge Base article:
306459 INFO: ASP.NET Server Controls Overview
For more information about how to extend WebForm controls to work with the ASP.NET validation controls, see the following Knowledge Base article:
310082 HOW TO: Extend a Web Form Control to Work with the Validation Controls by Using Visual Basic .NET

↑ Back to the top


Keywords: KB313044, kbvalidation, kbhowtomaster

↑ Back to the top

Article Info
Article ID : 313044
Revision : 7
Created on : 8/28/2007
Published on : 8/28/2007
Exists online : False
Views : 485