tailieunhanh - ASP.NET 4 Unleased - p 16
Using the Validation Controls From the Library of Wow! eBook Overview of the Validation Controls 125 A separate RequiredFieldValidator control is associated with each of the | 124 CHAPTER 3 Using the Validation Controls asp Label id lblProductQuantity Text Product Quantity AssociatedControlID txtProductQuantity Runat server br asp TextBox id txtProductQuantity Columns 5 Runat server asp RequiredFieldValidator id reqProductQuantity ControlToValidate txtProductQuantity Text Required Display Dynamic Runat server asp CompareValidator id CompareValidator1 ControlToValidate txtProductQuantity Text Invalid Quantity Operator DataTypeCheck Type Integer Runat server br br asp Button id btnSubmit Text Submit Product Order OnClick btnSubmit_Click Runat server fieldset asp Label id lblResult Runat server div form body html From the Library of Wow eBook Overview of the Validation Controls 125 A separate RequiredFieldValidator control is associated with each of the three form fields. If you attempt to submit the form in Listing without entering a value for a field a validation error message display see Figure . Each RequiredFieldValidator associates with a particular control through its ControlToValidate property. This property accepts the name of the control to validate on the page. CompareValidator controls associate with the txtProductPrice and txtProductQuantity TextBox controls. The first CompareValidator checks whether the txtProductPrice text field contains a currency value and the second CompareValidator checks whether the txtProductQuantity text field contains an integer value. There is nothing wrong with associating more than one validation control with a form field. If you need to make a form field required and check the data type entered into the form field you need to associate both a RequiredFieldValidator and CompareValidator control with the form field. Finally the property is checked in the Page_Load handler before the form data displays. When using the validation controls you should always check the property before doing anything with the data submitted to a page. This property returns the value true
đang nạp các trang xem trước