1 / 85

Chapter 3

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.)

lorin
Télécharger la présentation

Chapter 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Using Validation Controls

  2. 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

  3. Types of Validators • RequiredFieldValidator • RangeValidator • CompareValidator • RegularExpressionValidator • CustomValidator • Others

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. Listing 3.1 HTML

  9. Validator controls render HTML <span> tags. Initially, these are hidden. JavaScript code is created to handle client-side validation.

  10. 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.

  11. 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.

  12. 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.

  13. Listing 3.4 ShowValidators.aspx .aspx code

  14. Listing 3.4 ShowValidators.aspx Disabling client-side validation for the validators.

  15. 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.

  16. 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.

  17. Listing 3.4 ShowValidators.aspx Resulting page if a field is left unfilled.

  18. Listing 3.5 ShowValidationGroups.aspx .aspx code

  19. If you have two submit buttons, you can use the ValidationGroup property to only validate the controls relevant to a button.

  20. Browser’s initial render... Only the controls that are in the button’s ValidationGroup will be validated .

  21. <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>.

  22. 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

  23. 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

  24. Listing 3.9 ShowRangeValidator.aspx .aspx code

  25. Listing 3.10 ShowDataTypeCheck.aspx .aspx code

  26. Listing 3.11 ShowFixedValue.aspx .aspx code

  27. Listing 3.12 ShowCompareValues.aspx .aspx code

  28. Listing 3.13 ShowRegularExpressionValidator.aspx .aspx code

  29. Listing 3.13 ShowRegularExpressionValidator.aspx VALID EMAIL Resulting HTML sent to browser.

  30. Browser’s rendering of HTML code.

  31. Listing 3.13 ShowRegularExpressionValidator.aspx INVALID EMAIL Resulting HTML sent to browser.

  32. Browser’s rendering of HTML code.

  33. Listing 3.14 ShowCustomValidator.aspx .aspx code

  34. Listing 3.14 ShowCustomValidator.aspx INVALID COMMENT Resulting HTML sent to browser.

  35. Browser’s rendering of HTML code.

  36. Listing 3.14 ShowCustomValidator.aspx VALID COMMENT Resulting HTML sent to browser.

More Related