Browse DevX
Sign up for e-mail newsletters from DevX


Validating Data On Web Forms : Page 8

There are six validation controls included with ASP.NET as well as a summary control. Data validation with the ASP.NET validation controls is always processed on the Web server and can also optionally be processed on the user's computer.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Manually Enabling Validation Controls
The sample page for this article illustrates several examples of ASP.NET validation controls. Two of the validation controls on the page are accompanied by button controls that cause the page to be posted back to the Web server. When you're faced with multiple points on the page that can cause a post back on a Web form, managing validation controls can be tricky. It may be necessary to manage data validation controls on Web forms that have multiple separate sections displayed on them where each section can be posted back to the Web server, or submitted, separately.

For example, I've divided my sample page into several sections with each section illustrating a different data validation control. When a user clicks the first button control, if there is no data entered into the textbox control that is linked to the RequiredFieldValidator control, then the RequiredFieldValidator control returns false, displays its error message, and prevents the user from posting the page back to the Web server. You expect this behavior of the RequiredFieldValidator control, right? Here's the catch: the RequiredFieldValidator will behave in this manner regardless of where the page is posted back to the Web server from. Thus, if a user clicks the second button control, even though it is at a different point in the page, the RequiredFieldValidator at the top of the page will prevent the page from being posted back to the Web server.

The scenario above represents just a single example where it would be helpful to be able to manually and conditionally enable and disable the ASP.NET data validation controls. You can easily disable validation controls by simply setting the Enabled property of any of the validation controls to false. But, disabled validation controls will not perform data validation. However, validation controls that you've disabled can be enabled at any point that you need their services.

If you need to manage when data validation controls are enabled, a quick solution is to simply disable them by default. In the server-side function member that will be processed by the event that causes the page to be posted back to the Web server, you can take steps to validate the information that was submitted.

You first want to re-enable the needed validation controls. Next, call the Page.Validate function member provided by ASP.NET. This function member causes all of the validation controls on the Web form to validate the information submitted to the Web server. Finally, check the state of the Page.IsValid property. If any of the validation controls on the Web form were unsuccessful in the validation process, the Page.IsValid property will contain a value of false. The steps mentioned here give the developer complete manual and conditional control over when you want to enable the data validation controls. The snippet below illustrates the steps mentioned here.

public void SomeEventHandler() { rfvSample1.Enabled = true; Page.Validate(); if (Page.IsValid) { // Take some action based upon all data being valid. } else { // Take some other action based upon some some data being invalid. }

You can also manually and conditionally control data validation when you're performing client-side validation by simply having the script that performs the validation return a true or false. However, taking manual control over when validation occurs on the user's computer isn't common since the Web server will always process the validation controls.

Combining Validation Controls
Note that the RequiredFieldValidator control is the only data validation control that will fail the data validation process if the control that it is linked to contains no value. The CompareValidator, RangeValidator, RegularExpressionValidator, and CustomValidator will all pass validation if the value contained in the linked control is a valid value as well as if there is no value in the linked control. For this reason, you may need to combine multiple validation controls and link them to the same target control to be validated.

The validation controls are not actually combined in any way, but you can link multiple validation controls to the same target control to validate that target. In this manner, you can link both the RequiredFieldValidator control and one of the other validation controls, such as a RangeValidator control, to a TextBox control so that the TextBox must have a value entered in order to pass validation.

Validating the data entered by users is a vital part of any software application. Data validation processing prevents wasting time processing invalid data and guards the integrity of a data store by only allowing valid data to be entered into an application. Microsoft's ASP.NET data validation controls do a superb job standardizing the means of validating data entered by a user over the Web.

Shannon Horn is the co-founder and chief software architect of WebGeniuses Corporation. He is also a Microsoft Certified Application Developer (MCAD), Microsoft Certified Solution Developer (MCSD), and a Microsoft Certified Trainer (MCT) who has been developing Microsoft Windows and Web-based solutions as well as training for over 12 years. He has been a featured speaker at many industry events including Microsoft DevDays and asp.netPRO's ASP.NET and Web Services Solutions Conference. He is a published author on subjects such as XML, the migration from Visual Basic 6 to VB.NET, JScript.NET, C#, and ASP.NET. Shannon speaks and trains for companies such as AppDev and LearnIt and has been a featured speaker on training videos with LearnKey. He has also worked with large corporate clients including Microsoft, Universal Studios, MGM Studios, Monster.com/FlipDog.com, Intel, Polygram Pictures, Prudential, Micro Accounting Systems, Sky Harbor International Airport, and Southern Automated Systems on projects utilizing Microsoft technologies such as Visual FoxPro, Visual Studio .NET, C#, ADO.NET, and ASP.NET. Shannon lives in Glendale, AZ, and is married with two daughters and a son.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date