Example of validation control for ASP.net 2.0 server controls

Source: Internet
Author: User

In the previous article, we explained some of the knowledge about implementing validation controls. The content will provide a foundation for developers to implement validation controls. To help readers better understand how validation controls are implemented, this article first describes the implementation steps for common validation controls, and then demonstrates how to validate the controls by using a typical example.

1. Validation control Implementation steps

In the process of creating validation controls, you need to implement core content such as server-side validation and client validation. The following are examples of common implementation steps for validation controls.

(1) The validation control class should be inherited by the BaseValidator base class. This allows validation controls to automatically inherit the functionality required to participate in the validation framework. For example, by inheriting the BaseValidator ControlToValidate property, you implement the association of the validation control with the validation target control.

(2) Implement some properties that are specific to the validation control. Developers can define these unique properties from a convenient and practical perspective, based on application requirements.

(3) Rewrite the AddAttributesToRender method to add unique properties and related content to the control rendering. When implementing client-side validation code, you may need to refer to related rendering content.

(4) Rewrite the EvaluateIsValid method to add server-side validation logic to the validation control.

(5) Rewrite the event handling method for the PreRender event OnPreRender, in addition to invoking the base class implementation method, to implement the registration of client-side validation script files.

(6) Write a client-side validation script file packaged with the validation control and place it in the correct directory.

With one or more of these steps, the developer can implement a basic validation control. In the implementation process, the server-side validation logic and the client verification logic must be consistent, otherwise, even if the input data is validated by the client, it cannot be validated through the server side. The following is a typical example to further illustrate how validation controls are implemented to deepen the reader's understanding.

2. Typical application

In this section, you create a validation control named Telnumvalidator, using the method of developing the validation control described earlier. The control is used to verify that the phone number entered by the user conforms to the rule. If the rule is not met, then the dynamic prompts the error message. The effect is shown in Figure 1.

Figure 1 Effect chart (input error status)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.