ASP. NET-how to: Verify ASP. NET Server controls programmatically

Source: Internet
Author: User

By default, Asp.. Net verification control will automatically perform verification. If the browser supports client scripts, the control can also be verified in the browser.

However, sometimes you may need to perform verification programmatically. You may need to program the verification in the following cases:

· If the verification value is not set at runtime. For example, if you are using the rangevalidator control, you may need to set its minimumvalue and maximumvalue attributes at runtime based on the value entered by the user. In this case, the default verification does not work because the rangevalidator control does not have enough information when the page calls the verification control for verification.

· Determine the validity of controls (or the entire page) in the page_load event handler. In the page processing phase, the verification control has not been called, so the isvalid attribute of the page or independent control is not set. (If you try to obtain the value of this attribute, an exception is thrown .) However, to determine the validity, you can call verification programmatically.

· You can edit controls (or enter controls or verify controls) while running ).

 

More often, you can perform programming verification as needed to achieve more precise control during verification execution.

 

Call the validate method of the verification control.

The control checks and sets the isvalid attribute of the control and page. If an error is detected, the error message is displayed as usual when the page returns to the user.

 

The following code example shows how to set attributes programmatically. In this case, an ASP. NET web page is used to reserve a room at a resort to provide free round-trips during each visit. The user must enter the date of arrival and departure, and then schedule the visit period. The rangevalidator control is used to ensure that users enter a typical date format. The travel date should be between the arrival and departure dates.

Note: If the value entered by the user cannot be converted to a date, the verification program control will cause an exception. For clarity, this example does not contain the error handling section.

The arrival and departure dates come from two textbox web server controls on the page, txtarrival and txtdeparture. Enter the travel date in the third Textbox Control, that is, txttourdate, which is verified by the rangevalidator control.

Note: client scripts should be disabled for verification by programming so that the control does not display incorrect error messages before your server verifies code execution.

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

{

Rangevalidator1.minimumvalue = txtarrival. text;

Rangevalidator1.maximumvalue = txtdeparture. text;

Rangevalidator1.type = validationdatatype. date;

Rangevalidator1.validate ();

 

If (! Rangevalidator1.isvalid)

{

Rangevalidator1.errormessage = "the tour date must" +

"Fall between the arrival and departure dates .";

}

}

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.