Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Using Validation Controls. What is a Validation Control? A control that validates the value in another control Renders as an HTML tag with an.

Similar presentations


Presentation on theme: "Chapter 3 Using Validation Controls. What is a Validation Control? A control that validates the value in another control Renders as an HTML tag with an."— 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 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 A RequiredFieldValidator is used to ensure that required fields are not left blank. All Validator controls have two important properties: 1.ControlToValidate 2.Text

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 leaves a required field blank. If user puts wrong type of data into a field. 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 tags. Initially, these are hidden. JavaScript code is created to handle client-side validation.

10

11 Listing 3.2 ValidationImage.aspx You can make a Validator display an image instead of text by using an HTML tag in the Text property.

12

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

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

15 Listing 3.4 ShowValidators.aspx.aspx code

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

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

18 Listing 3.4 ShowValidators.aspx void Page_PreRender() { foreach (BaseValidator valControl in Page.Validators) { WebControl assControl = (WebControl)Page.FindControl(valControl.ControlToValidate); if (!valControl.IsValid) assControl.BackColor = System.Drawing.Color.Yellow; else assControl.BackColor = System.Drawing.Color.White; } BaseValidator is the root class for all validators. 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. 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.

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

20

21 Listing 3.5 ShowValidationGroups.aspx.aspx code

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

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

24 is an HTML element. It renders as a box with a line border surrounding its HTML sub-elements. renders the text that appears in the border of a.

25

26 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

27

28

29 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

30

31 Listing 3.9 ShowRangeValidator.aspx.aspx code

32

33 Listing 3.10 ShowDataTypeCheck.aspx.aspx code

34

35 Listing 3.11 ShowFixedValue.aspx.aspx code

36

37 Listing 3.12 ShowCompareValues.aspx.aspx code

38

39

40

41 Listing 3.13 ShowRegularExpressionValidator.aspx.aspx code

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

43 Browser’s rendering of HTML code.

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

45 Browser’s rendering of HTML code.

46

47 Listing 3.14 ShowCustomValidator.aspx.aspx code

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

49 Browser’s rendering of HTML code.

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

51 Browser’s rendering of HTML code.

52

53 Listing 3.16 ShowValidateEmptyText.aspx.aspx code

54 Listing 3.16 ShowValidateEmptyText.aspx Resulting HTML sent to browser. INVALID PRODUCT CODE

55 Browser’s rendering of HTML code.

56 Listing 3.16 ShowValidateEmptyText.aspx Resulting HTML sent to browser. VALID PRODUCT CODE

57 Browser’s rendering of HTML code.

58

59 Listing 3.17 TimedTest.aspx.aspx code

60

61 Listing 3.17 TimedTest.aspx Resulting HTML sent to browser. BEFORE SUBMISSION

62 Browser’s rendering of HTML code.

63 Listing 3.17 TimedTest.aspx Resulting HTML sent to browser. AFTER SUBMISSION

64 Browser’s rendering of HTML code.

65

66 Listing 3.18 ShowValidationSummary.aspx.aspx code

67 Listing 3.18 ShowValidationSummary.aspx Resulting HTML sent to browser. BEFORE SUBMISSION

68 Browser’s rendering of HTML code.

69 Listing 3.18 ShowValidationSummary.aspx Resulting HTML sent to browser. AFTER SUBMISSION

70 Browser’s rendering of HTML code.

71

72 Listing 3.19 ShowSummaryPopup.aspx.aspx code

73 Listing 3.19 ShowSummaryPopup.aspx Resulting HTML sent to browser. BEFORE SUBMISSION

74 Browser’s rendering of HTML code.

75

76 Listing 3.21 ShowLengthValidator.aspx.aspx code

77 Listing 3.21 ShowLengthValidator.aspx Resulting HTML sent to browser. BEFORE SUBMISSION

78 Browser’s rendering of HTML code.

79 Listing 3.21 ShowLengthValidator.aspx Resulting HTML sent to browser. AFTER SUBMISSION

80 Browser’s rendering of HTML code.

81

82 Listing 3.24 ShowAjaxValidator.aspx.aspx code

83

84 Listing 3.24 ShowAjaxValidator.aspx Resulting HTML sent to browser.

85 Browser’s rendering of HTML code.


Download ppt "Chapter 3 Using Validation Controls. What is a Validation Control? A control that validates the value in another control Renders as an HTML tag with an."

Similar presentations


Ads by Google