850 likes | 1.05k Views
Chapter 3. Using Validation Controls. What is a Validation Control?. A control that validates the value in another control Renders as an HTML <span> tag with an error message Each validation control is associated with one specific ControlToValidate (e.g. Textbox, DropdownList, etc.)
E N D
Chapter 3 Using Validation Controls
What is a Validation Control? • A control that validates the value in another control • Renders as an HTML <span> tag with an error message • Each validation control is associated with one specific ControlToValidate (e.g. Textbox, DropdownList, etc.) • A particular web control can have multiple different types of validators
Types of Validators • RequiredFieldValidator • RangeValidator • CompareValidator • RegularExpressionValidator • CustomValidator • Others
Listing 3.1 OrderForm.aspx • All Validator controls have two important properties: • ControlToValidate • Text A RequiredFieldValidator is used to ensure that required fields are not left blank.
A CompareValidator is can be used to perform various comparison operations. In this case we are comparing the data entered to a desired data type. We are ensuring that the user actually enters numbers for the price and quantity.
When page first loads... If user enters valid data and clicks the submit button, the page will post to the server and the button’s OnClick event handler will be triggered.
If user puts wrong type of data into a field. If user leaves a required field blank. Handled via a RequiredFieldValitdator. Handled via a CompareValidator. NOTE: in these cases, the page will NOT post. Validation checks are handled at the client, not the server.
Validator controls render HTML <span> tags. Initially, these are hidden. JavaScript code is created to handle client-side validation.
Listing 3.2 ValidationImage.aspx You can make a Validator display an image instead of text by using an HTML <img> tag in the Text property.
Listing 3.3 ShowSetFocusOnError.aspx The SetFocusOnError property can be used to guide the user to the control that needs correcting by automatically setting its focus.
Server-side Validation • By setting a Validator’s EnableClientScript property to false, you can force a post and therefore handle the validation in your server-side script. • In the server’s event handler, you can write special validation code in the PreRender event handler. • Use the Page object’s Validators collection.
Listing 3.4 ShowValidators.aspx .aspx code
Listing 3.4 ShowValidators.aspx Disabling client-side validation for the validators.
Listing 3.4 ShowValidators.aspx In the Page_PreRender event handler, loop through the page’s validators, check to see if they pass the validation test, and if not manipulate properties of their associated controls.
Listing 3.4 ShowValidators.aspx BaseValidator is the root class for all validators. void Page_PreRender() { foreach (BaseValidatorvalControlin Page.Validators) { WebControlassControl = (WebControl)Page.FindControl(valControl.ControlToValidate); if (!valControl.IsValid) assControl.BackColor = System.Drawing.Color.Yellow; else assControl.BackColor = System.Drawing.Color.White; } } All validators have a boolean IsValid property that indicates whether the validation test succeeds. This can be used to make decisions on what to do with associated controls. All validators have a ControlToValidate property that has the name of the associated control. Given the name you can get a reference to the control using the FindControl method. This allows you to manipulate the control’s properties.
Listing 3.4 ShowValidators.aspx Resulting page if a field is left unfilled.
Listing 3.5 ShowValidationGroups.aspx .aspx code
If you have two submit buttons, you can use the ValidationGroup property to only validate the controls relevant to a button.
Browser’s initial render... Only the controls that are in the button’s ValidationGroup will be validated .
<fieldset> is an HTML element. It renders as a box with a line border surrounding its HTML sub-elements. <legend> renders the text that appears in the border of a <fieldset>.
Listing 3.6 ShowDisableValidation.aspx By setting the CausesValidation property to false, you can cause a button click to bypass validation and directly post. Useful for a Cancel button
Listing 3.8 ShowInitialValue.aspx A DropDownList with an associated RequiredFieldValidator. If the control’s current value is equal to the validator’s InitialValue property, the validation test fails. Clicking without selecting Clicking after select
Listing 3.9 ShowRangeValidator.aspx .aspx code
Listing 3.10 ShowDataTypeCheck.aspx .aspx code
Listing 3.11 ShowFixedValue.aspx .aspx code
Listing 3.12 ShowCompareValues.aspx .aspx code
Listing 3.13 ShowRegularExpressionValidator.aspx VALID EMAIL Resulting HTML sent to browser.
Listing 3.13 ShowRegularExpressionValidator.aspx INVALID EMAIL Resulting HTML sent to browser.
Listing 3.14 ShowCustomValidator.aspx .aspx code
Listing 3.14 ShowCustomValidator.aspx INVALID COMMENT Resulting HTML sent to browser.
Listing 3.14 ShowCustomValidator.aspx VALID COMMENT Resulting HTML sent to browser.