Transcript unit2.2
MIS 3200 – Unit 2.2
• Outline – What’s the problem with bad data?
– Traditional solutions – ASP.NET tools – Validators • RequiredFieldValidator • RangeValidator • CompareValidator • ValidationSummary
What’s the problem
• BAD DATA!
– Bad input data • Incorrect format • Illegal values • Illogical values • Illegal characters 03/30/12 -5/03/2012 30/03/2012 ##/03/2012 – Illegal operations (e.g. division by zero) 03/30/2013
Traditional Solutions
• • • • Data entry professionals Double entry procedures Self-checking numbers Program code
Solution: Validators
• Controls that do error checking, such as – Require an entry: • Provide a credit card number – Within a specified range • Year of birth should be between 1900 and 2000 – Compare an entry to a predetermined value • Retype your password, and they should match – Compare an entry the value of another control • Require an email address to contain “@” and “.”
How Validators Work
• • • • ControlToValidate property – tells the validator which TextBox to validate Placed next to the control they to validate User feedback –
Text
–
ErrorMessage
Remember these user feedback properties “Fire” (be triggered) when a button is clicked or when focus passes to another control.
RequiredFieldValidator - RFV
• • • Typically associated with a TextBox Fires if nothing is entered in the TextBox When the validator fires it displays a message – If only one of the validator’s ErrorMessage and Text properties has a value then that value is displayed – If both ErrorMessage and Text have values then the value of Text is displayed at the location of the validator and the value of ErrorMessage is displayed in a ValidationSummary control if there is one on the page
RFV Example
txtNum1 Running version before pressing Try It Running version after pressing Try It Error message color set to red No value set for Text
RFV Example 2
• Validation problems often need more text than what can be accommodated next to the web control.
• Use ValidationSummary control and the ErrorMessage property linked to all validators on the page Design view After Running and pressing Try It
CompareValidator - CV
• • • Compare an entry to a predetermined value – ControlToCompare if you want to compare to another Control or the value of another control – ValueToCompare to compare to a predetermined value To prevent missing data while comparing, – MUST be used with a RequiredFieldValidator – CV only fires if there is something to validate
CV Example
• Allows you to chose the data type – Default type is string • • Allows you to chose the type of comparison – Default is a test for equality Allows you to set focus on the problematic data when the validator fires – Recall: the Focus() method
CV Example
To compare to another control Entry is not > 10 RequiredFieldValidator CompareValidator Entry is a whole number > 10 Entry is not a whole number
RangeValidator - RV
• • • Similar to the CompareValidator but has a – MinimumValue and a MaximumValue (instead of a ValueToCompare or FieldToCompare) Has the same Type choices as the CV Has the same SetFocusOnError option
RV Example
• • An entry is required Please enter a number in the range RFV and RV Note: this works fine for data types double and decimal
Try it Out – L2.2
1. Make sure ASPPub is on your desktop 2. Start VWD and open ASPPub 3. Open the Unit2 folder under MIS3200 4. Click on yourLastNameU2L2.aspx file 5. Right-click and copy 6. Click on the Unit2 folder 7. Right-click and paste 8. Change the file name to yourLastNameU2L22.aspx
L2.2 #2
9. Open the file in Design view 10. Drop a RequiredFieldValidator to the right of your first TextBox a. Change the (ID) to rfvNum1 b. Change the ControlToValidate to txtNumber1 c. Change the ErrorMessage to Number 1 is required d. Change the Text to * e. Change ForeColor to
red
f.
Change SetFocusOnError to true g. Change the Display to Dynamic
L2.2 #3
11 Drop a CompareValidator to the right of your RequiredFieldValidator a. Set the (ID) to cvNumber1 b. Set ControlToValidate to txtNumber1 c.
Change ErrorMessage to Number 1 must be a valid number d. Change Text to * e. Change SetFocusOnError to true f.
Change the ForeColor to
red
g. Change Operator to DataTypeCheck h. Change Type to Double i.
Change the Display to Dynamic
L2.2 #4
12 Repeat steps 10 and 11 for the second TextBox, changing names and messages as required 13 Drop another CompareValidator after the second TextBox 14 Repeat all of step 11 again for the 2 nd differences: CV with the following a. Set the (ID) to cvNumber2Zero b. Set the error message to Number 2 can’t be zero c. Set Operator to NotEqual d. Set ValueToCompare to 0 (a zero) e. Set the ControlToValidate to txtNumber2
f.
Do NOT set the Type
(if you do delete the validator and repeat)
L2.2 #5
15 Drop a ValidationSummary after lblOutput a. Change its ForeColor to
red
16 Run the page trying various combinations of values a. Press + with nothing in the text boxes b. Put a number in one text box but not the other c. Put non-numeric data in one text box or the other d. Put zero in the second text box 17 Change the page heading to
Unit 2.2 L2.2 – Simple Calculator with Validation
L2.2 – Sample Output
L2.2 – Final steps
18 Open your MIS3200 home page in the MIS3200 folder 19 Add another hyperlink for unit 2 a. (ID) = hlU22L22 b. Text = Unit 2.2 L2.2
c. NavigateUrl = the page you just modified 20 Copy ASPPub back to ASPNET and submit your MIS Portfolio URL to the drop box
Think About It!
• • Why are validators important?
When do you use the following: – RequiredFieldValidator – RangeValidator – CompareValidator – ValidationSummary