Danh mục

Validate Data Using Validation Controls

Số trang: 6      Loại file: pdf      Dung lượng: 35.27 KB      Lượt xem: 5      Lượt tải: 0    
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Xác nhận dữ liệu Sử dụng điều khiển Xác Nhận Tôi muốn để có thể xác nhận các loại hình nhập dữ liệu mà không cần phải chờ đợi cho một lỗi trở về từ máy chủ. Tôi có thể xác nhận dữ liệu của tôi trên Form Web của tôi?
Nội dung trích xuất từ tài liệu:
Validate Data Using Validation Controls 5.2 Validate Data Using Validation ControlsI want to be able to validate various types of data entry without having to wait for anerror to come back from the server. Can I validate my data on my Web Form?Note A note of caution: Through the writing and tech editing of this chapter, there have been some inconsistencies noted in the reaction of some of the validator controls. The tech editor and I both contend that this area might have some .NET bugs. This is also the reason for the lack of coverage in the CustomValidator.TechniqueOne new feature found in ASP.NET is the inclusion of Validation Web server controls.These validation controls allow you to specify other controls of which you want tovalidate based on data entered into the controls. You can then do the following: • Have the Validation control display an error message. • Test at a page level to see if all controls that are being validated are valid. • Display a list of error messages for all the controls that are being validated.Available Validation Web Server ControlsYou will find the controls for validation in the toolbox, and can see them listed in Table5.2, with a description of what they validate. Table 5.2. Validation Web Server ControlsControl DescriptionRequiredFieldValidator Validates whether the specified control has been left blank.CompareValidator Compares the values in two controls and validates whether they are equal.RangeValidator Checks to see if the value entered into a control falls within a range that is specified.RegularExpressionValidator Compares a value entered into a control to see if it matches an entered mask, such as 999-99-9999, which is the U.S. Social Security Number.CustomValidator Allows you to create custom functions on both the client and server side for validation.ValidationSummary Used to consolidate the messages that all validation controls return on a page into a list-like format.The main properties you will set on a validation control are FieldToValidate andErrorMessage.Note The CustomValidator control is different in that you will create functions to perform the validation. Other than these, the validation controls require no coding unless you want to validate the whole page.Testing Page Validation in CodeDepending on what is required, you can test for validation at the page level using thePage.Validate method. When you call this method, all the Validation controls areretested, and the Page.IsValid property is set. You can use the IsValid property in acondition statement to perform tasks based on the value of the IsValid property, as shownin the last step of this How-To.StepsOpen and run the Visual Basic .NET-Chapter 5 solution. From the main page, click onthe hyperlink with the caption How-To 5.2: Validate Data Using Validation Controls.When the Web Form loads, you will see a number of text boxes with instructions on howto see the various validation checks. Here is the Web Form in Design view (see Figure5.2). 1. Create a Web Form. Then place the controls listed in Table 5.3 and shown in Figure 5.2 with the following properties set. Table 5.3. Control Property Settings for Validation Controls Web Form Object Property Setting Label Text Required Field Validator Example (Leave Blank to Test)TextBox ID txtRequiredExampleLabel Text Compare Validator Example (Enter Two Different Values in These Text Boxes)TextBox ID txtCompareFirstTextBox ID txtCompareSecondLabel Text Range Validator Example (Enter a Number Outside the Range 1 and 10)TextBox ID txtRangeExampleLabel Text Regular Expression Validator Example (Enter a SSN not matching 999-99- 9999)TextBox ID txtRegularExprExampleLabel Text Custom Validator Example (Enter a State Other Than WA or CA)TextBox ID txtCustomExampleButton ID btnTestValidators Text Test ValidatorsLabel Text Page Validation ErrorsRequiredFieldValidator ToValidate txtRequiredExampleControl ErrorMessage Here is the message for the Required Field ValidatorCompareValidator ControlToCompare txtCompareFirst ControlToValidate txtCompareSecond ErrorMessage Here is the message for the Compare ValidatorRangeValidator ControlTo ...

Tài liệu được xem nhiều: