Wednesday, July 18, 2007

Adding client side java script code to a ASP.NET Control

1. Adding client side (ie java script) code to a Control
A. Example. “Are you Sure?” message box on Delete button
B. lnkDelete.Attributes.Add("onClick", "return confirm('really delete ?')")
1. Will not post back without an OK.
C. This should go in the Page_Load.
2. Validation Controls: writes client side java script for data validation
A. Prior to .NET you wrote client side Java script
1. And server side script just in case the browser couldn’t run java
B. .Net now generates both Server & Client side java using controls
C. Key point…this is client side validation…no round trip!
D. Place these controls where you want the error message to appear
1. ControlToValidate (required) defines which control to check.
2. ErrorMessage is what appears when the local input is invalid
3. Text property overrides ErrorMessage and is used with summary control
E. Can have more than one validate control per item you are validating
1. Set the Display = dynamic
3. RequiredFieldValidation Control: One for each control to validate
A. Must fill in ControlToValidate and ErrorMessage
B. ErrorMessage: Actual error message
C. Text: (text to display…defaults to errormessage if blank)
1. Used with summary control
D. Compares ‘initial value’ against Users value…if different then OK
1. Default is blank initial value so control requires some input
a) BUT you can put in both the control and initial value
b) Interesting….
E. Page will not PostBack with invalid entry.
4. RangeValidator Control
A. Checks max, min
1. NOT used to just check data type…use compareValidator
B. Can modify max and min in code in page load so it’s relative to today
1. rvalBirthDate.MinimumValue = CStr(Today.AddYears(-70))
a) This says can not be older than 70
b) Note the use of the Today object
5. RegularExpressionValidator; Pattern matching
A. Validating expressions…ie phone or e-mail formats
1. Built in formats and ability to build your own (doable but requires some study)
2. Can test against a list with CA|NV|AZ
6. CustomValidator
A. Call your own code to validate
1. Must write both the server & client side code
2. Remember….validate controls run on the client!
B. Double click the custom validate control to write the server side
1. args.value (value in the control)
2. args.IsValid = True means it’s OK!
C. Write the client side code in the design HTML form
a) Tip…copy server side code into script tags in client side
(1) Edit out stuff VB script doesn’t like
2. Give it any name but you must have same (source, arguments)
7. CompareValidator
A. Compare to a value or another control
1. IE the two entries for changing a password
B. Operator property can also be used to:
1. Compare entry = to a value
2. Compare entry <> and not equal to
C. Operator = DataTypeCheck just to make sure it’s the valid data type
1. Type = set to data type to check for.
2. The client side code procedure name has to be included in the ClientValidationFunction property of the control
8. ValidationSummary control
A. Place all error messages in one place…and use * next to control with error
B. Easy…replace all text properties (not error messages) with * for all validation controls
C. Insert the ValidationSummary control at the bottom
D. Error messages will appear when the page PostBack.
E. Set the DisplayMode to change looks
F. Set Visible to False…and ShowMessageBox…true…= popup