Sunday, 5 May 2013

ASP.NET - Validation Server Controls

Validation server controls are used to validate user-input. If the user-input does not pass validation, it will display an error message to the user.
Each validation control performs a specific type of validation (like validating against a specific value or a range of values).
By default, page validation is performed when a Button, ImageButton, or LinkButton control is clicked. You can prevent validation when a button control is clicked by setting the CausesValidation property to false.
The syntax for creating a Validation server control is:
< asp:control_name id="some_id" runat="server" />
In the following example we declare one TextBox control, one Button control, and one RangeValidator control in an .aspx file. If validation fails, the text "The value must be from 1 to 100!" will be displayed in the RangeValidator control:

Example

< html>
< body>

< form runat="server">
< p>Enter a number from 1 to 100:
< asp:TextBox id="tbox1" runat="server" />
< br /><br />
< asp:Button Text="Submit" runat="server" />
< /p>

< p>
< asp:RangeValidator
ControlToValidate="tbox1"
MinimumValue="1"
MaximumValue="100"
Type="Integer"
Text="The value must be from 1 to 100!"
runat="server" />
< /p>
< /form>

< /body>
< /html>

No comments:

Post a Comment