《web应用开发-5概要》由会员分享,可在线阅读,更多相关《web应用开发-5概要(21页珍藏版)》请在金锄头文库上搜索。
1、Module 5: Validating User Input,Overview,Overview of User Input Validation Using Validation Controls Page Validation,Lesson: Overview of User Input Validation,What Is Input Validation? Client-Side and Server-Side Validation ASP.NET Validation Controls,What Is Input Validation?,Verifies that a contro
2、l value is correctly entered by the user Blocks the processing of a page until all controls are valid Avoids spoofing or the addition of malicious code,Client-Side and Server-Side Validation,ASP.NET can create both client-side and server-side validation Client-side validation Dependent on browser ve
3、rsion Instant feedback Reduces postback cycles Server-side validation Repeats all client-side validation Can validate against stored data,Valid?,Valid?,User Enters Data,No,No,Yes,Yes,Error Message,Client,Server,Web Application Processed,ASP.NET Validation Controls,ASP.NET provides validation control
4、s to: Compare values Compare to a custom formula Compare to a range Compare to a regular expression pattern Require user input Summarize the validation controls on a page,Lesson: Using Validation Controls,Adding Validation Controls to a Web Form Positioning Validation Controls on a Web Form Combinin
5、g Validation Controls Input Validation Controls Using the RegularExpressionValidator Control Demonstration: Using Validation Controls Using the CustomValidator Control Demonstration: Using the CustomValidator Control,Adding Validation Controls to a Web Form,Add a validation control Select the input
6、control to validate Set validation properties,1,2,3,Positioning Validation Controls on a Web Form,Create error messages Select display mode Static Dynamic,Combining Validation Controls,Can have multiple validation controls on a single input control Only the RequiredFieldValidator checks empty contro
7、ls,Input Validation Controls,RequiredFieldValidator InitialValue CompareValidator ValueToCompare or ControlToCompare Type Operator RangeValidator MinimumValue MaximumValue Type,Code Examples,Using the RegularExpressionValidator Control,Used when input must conform to a pre-defined pattern Visual Stu
8、dio .NET includes patterns for: Telephone numbers Postal codes E-mail addresses,*,Code Example,Demonstration: Using Validation Controls,Create an ASP.NET Web Form with TextBox and Button controls Add a RequiredFieldValidator control Add a RangeValidator control Add a RegularExpressionValidator contr
9、ol,Using the CustomValidator Control,Can validate on client-side, server-side, or both ClientValidationFunction OnServerValidate Validate with: Formula Data COM objects Web Service,Code Example,Demonstration: Using the CustomValidator Control,Add a CustomValidator control Write the server-side code
10、Write the client-side script Test the result,Lesson: Page Validation,Using the Page.IsValid Property Using the ValidationSummary Control Demonstration: Using the Page.IsValid Property and the ValidationSummary Control,Using the Page.IsValid Property,Polls all validation controls,Sub cmdSubmit_Click(
11、s As Object, e As EventArgs) If Page.IsValid Then Message.Text = “Page is valid!“ Perform database updates or other logic here End If End Sub,private void cmdSubmit_Click(object s, System.EventArgs e) if (Page.IsValid) Message.Text = “Page is Valid!“; / Perform database updates or other logic here ,
12、Using the ValidationSummary Control,Collects error messages from all validation controls on the page Can display text and error messages Use Text=“*“ to indicate the location of the error,Demonstration: Using the Page.IsValid Property and the ValidationSummary Control,Open an ASP.NET page with multi
13、ple TextBox and validation controls Add a ValidationSummary control Add a script that uses the Page.IsValid property,Review,Overview of User Input Validation Using Validation Controls Page Validation,Medical Medical.aspx,Lab 7: Validating User Input,Benefits Home Page Default.aspx,Life Insurance Lif
14、e.aspx,Retirement Retirement.aspx,Dental Dental.aspx,Dentists,Doctors Doctors.aspx,Doctors,Logon Page Login.aspx,Registration Register.aspx,Coho Winery,Prospectus Prospectus.aspx,XML Web Service dentalService1.asmx,Page Header Header.ascx,ASPState,tempdb,Lab Web Application,User Control namedate.ascx,Menu Component Class1.vb or Class1.cs,XML Files,Web. config,