What are validation controls in ASP.NET? How do validation controls work? What are validation groups in ASP.NET 2?
An interview in ASP NET can't be really complete without preparing this question.
Validation controls in ASP.NET are server side controls that validate input values client-side. Sounds strange?
Well, the best approach to validate any input value is to validate it client-side to avoid any postback and
load to the server. This approach is followed to reduce the load on the server.
ASP.NET validation controls greatly improve the perfomance of a web application and may be used
by web developers to reduce plenty of code that they used to write previously to validate input values
Input validations improvise security of a web application, by preventing SQL Injection attacks.
There are 6 validations controls in both ASP.NET 1.1 and ASP.NET 2.0
1) RequiredFieldValidator - as the name suggests, this control makes sure that the input
box is not left blank.
2) CompareValidator - This control validates values in two controls, and checks for equality
3) RangeValidator - This validation control makes sure that the value entered in the control falls
within a range specified. This specification may be done using its properties.
4) RegularExpression - Based on a regular expression, values entered in an input box must match the
ones specified by this control's RegulareExpression property
5) CustomValidator - This type of validator control may be customized & created by a developer as per need
6) ValidationSummary - This control may be used to show the summary of the information gathered using rest
of the other validation controls
To use a validation control, set a validation control on a form. Associate it with a server control
by using its ControlToValidate property. Note that the server control has a property called
CausesValidation and this is set to true for the validation control to trigger. This property
is true by default for every server control. The validation
control also has an ErrorMessage property that is assigned some text to explain what the error is, in
case the validation control is trigerred. The validation control actually calls a client-side script
that comes with ASP.NET, and this script does the job of validation for us. All this validation is done
Suppose we want to use the RequiredFieldValidator control so that a textbox is'nt left blank, for this, see
the inline code below...
Code used for RequiredFieldValidator used above
<asp:Button ID="btnGo" runat="server" Text="Go" />
<asp:TextBox ID="txtSomeText" runat="server" Text="" CausesValidation="true"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTextbox" runat="server" ControlToValidate="txtSomeText"
ErrorMessage="Please Enter Some Text" ></asp:RequiredFieldValidator>
The Page.IsValid property of the page is TRUE only if there are no validation errors returned.
Values Between Pages
Page Life Cycle
Validation controls are supported by Internet Explorer, Firefox and Opera.
ASP.NET 2.0 provides a method to allow multiple sets of controls that may be grouped together into a validation
group. The purpose of this approach makes it easy to invoke validation checks on specific server controls and
not all ot them who have an associated validation control.