By default, ASP.NET validation controls automatically perform validation when the page posts back to the server, after the page is initialized (that is, after the view state and postback data have been processed), and before event-handling code is called. If the browser supports client-side scripting, the control can also perform validation in the browser.
However, sometimes you may need to perform validation programmatically. You may need to verify programmatically in the following situations:
·If the validation value has not been set at run time. For example, if you are using a RangeValidator control, you may want to set its MinimumValue and MaximumValue properties at run time based on the value entered by the user. The default validation does not work at this time because there is not enough information in the RangeValidator control when the page calls the validation control to perform validation.
·Need to determine the validity of the control (or the entire page) in the Page_Load event handler. During the processing phase of the page, the validation control has not yet been called, so the IsValid property of the page or individual controls has not been set. (If you try to get the value of this property, an exception will be thrown.) But if you want to determine validity, you can call validation programmatically.
·You are editing a control (either an input control or a validation control) at runtime.
More commonly, you can verify programmatically any time you want, giving you more precise control over when verification is performed.
Call the Validate method of the validation control.
The control performs checks and sets the IsValid property of the control and page. If an error is detected, the error message will be displayed as usual when the page is returned to the user.
The following code example demonstrates how to set properties programmatically. In this case, an ASP.NET web page books a room at a resort, providing free travel to and from each visit. Users must enter arrival and departure dates and then plan their schedule during the visit. The RangeValidator control is used to ensure that the user enters a typical date format and that the travel date should be between the arrival and departure dates.
Description: The validator control throws an exception if the value entered by the user cannot be converted to a date. For clarity, this example does not include error handling.
The arrival and departure dates come from two TextBox Web server controls on the page, txtArrival and txtDeparture. The travel date is entered into the third TextBox control, txtTourDate, which is validated by the RangeValidator control.
Note: When validating programmatically, client-side scripting should be disabled so that the control does not display incorrect error messages before your server-side validation code is executed.
private void Button1_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.";
}
}
This article comes from the CSDN blog. Please indicate the source when reprinting:
http://blog.csdn.net/dodream/archive/2009/12/17/5024666.aspx