ASP. NET-how to: test the validity of ASP. NET Server controls programmatically

Source: Internet
Author: User

Verify the control to test user input, set error status, and generate error information. They do not change the page processing process. For example, the Code is skipped when a user input error is detected. Instead, you will test the control status in the code before executing the specific logic of the application. If an error is detected, your code cannot run. The page continues to process the error and returns the error message to the user.

Security Description: by default, ASP. NET web pages automatically verify that malicious users attempt to send scripts or HTML elements to your applications.

You can test the status of a regular page range and the status of a separate control. Generally, you complete the above work in the event handler created for the page.

 

I. Test the normal error status

In your code, test the isvalid attribute of the page. This property accumulates the values of the isvalid attribute of all the verification controls on the page (using logic and). If any verification control is set to invalid, the page property returns false.

Note: verification information is unavailable during page initialization or loading. However, you can manually call the validate method during page_load and then test the isvalid attribute of the page.

The following code example demonstrates the event handler of the button. Isvalid attribute of the Code test page. Note that no other clause is required here because the page is automatically returned to the browser and the verification control displays its own error message.

Void button#click (Object sender, system. eventargs E)

{

If (isvalid)

{

// Perform database updates or other logic here.

}

}

 

2. Test the error status of a separate control

Click the validators set on the page, which contains references to all verification controls. You can then check the isvalid attribute of each verification control.

Note: To perform this check during page_load, you must manually call the validate method.

 

The following code example shows how to obtain the status of a single verification control.

If (this. ispostback)

{

This. Validate ();

If (! This. isvalid)

{

String MSG = "";

// Loop through all validation controls to see which

// Generated the errors.

Foreach (ivalidator avalidator in this. validators)

{

If (! Avalidator. isvalid)

{

MSG + = "<br/>" + avalidator. errormessage;

}

}

Label1.text = MSG;

}

}

 

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.