Browse DevX
Sign up for e-mail newsletters from DevX


Validating Data On Web Forms : Page 2

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

RequiredFieldValidator Control
The most commonly used ASP.NET data validation control is the RequiredFieldValidator control. To use it, you link the RequiredFieldValidator control to another control, such as a textbox, and use it to determine if the linked control has a value entered into it or it is empty. If the linked control has a value entered into it, data validation will succeed. Alternatively, if the linked control is empty, data validation will fail.

You'll mostly use RequiredFieldValidator controls in conjunction with textbox controls. In the snippet that follows I've indicated the HTML to generate both an asp:TextBox control and a linked asp:RequiredFieldValidator control.

<asp:TextBox ID="txtName" RunAt="Server" /> <asp:RequiredFieldValidator ControlToValidate="txtName" Display="Static" ErrorMessage="Name is required." ID="rfvName" RunAt="Server" />

In the snippet above I've declared a textbox with an ID attribute called txtName. The RequiredFieldValidator linked to it has an ID attribute called rfvName and I've included some of the most commonly used attributes. I used the ControlToValidate attribute to link RequiredFieldValidator to the textbox. The RunAt="Server" attribute is required for all ASP.NET Web server controls.

The Display attribute determines how space is reserved to display the validation control message if data validation fails. The value of Static specifies that ASP.NET will allocate the space on the page when the page is rendered. Static is the default value. Other possible values for the Display attribute are Dynamic and None.

The HTML rendered by the ASP.NET validation controls uses a script library called WebUIValidation.js and implements Microsoft JScript functionality that is only supported by Microsoft Internet Explorer 4.0 and later versions.
A Display attribute value of Dynamic specifies that you don't want ASP.NET to allocate any space on the page ahead of time to display a validation control message. If validation fails, ASP.NET will dynamically allocate space on the page on the fly. If you choose the Dynamic value and your validation fails, you could cause visual layout problems on pages that use HTML tables for page layout. If your validation control is contained inside of a table cell, the table cell will dynamically expand to accommodate the display of the validation error message.

When you choose to set Display="None" this specifies that no error message will display for the validation control regardless of the outcome of the data validation process. The None value is useful when you use it in combination with the ValidationSummary control that I'll illustrate later in this article. Figure 2 shows an excerpt from the sample page output from the HTML snippet shown above.

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