Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [205]

By Root 5547 0
are MinimumValue and MaximumValue, which together clearly denote the lower and upper boundaries of the interval. Note that an exception is thrown if the strings assigned MinimumValue or MaximumValue cannot be converted to the numbers or dates according to the value of the Type property.

If the type is set to Date, but no specific culture is set for the application, you should specify dates using a culture-neutral format, such as yyyy-MM-dd. If you don’t do so, the chances are good that the values will not be interpreted correctly.

Note

The RangeValidator control extends the capabilities of the more basic CompareValidator control by checking for a value in a fixed interval. In light of this, the RangeValidator control might raise an exception if either MinimumValue or MaximumValue is omitted. Whether the exception is thrown or not depends on the type chosen and its inherent ability to interpret the empty string. For example, an empty string on a Date type causes an exception. If you want to operate on an unbound interval—whether it’s lower or upper unbound—either you resort to the GreaterThan (or LessThan) operator on the CompareValidator control or simply use a virtually infinite value such as the 9999-12-31 value.

The RequiredFieldValidator Control


To catch when a user skips a mandatory field in an input form, you use the RequiredFieldValidator control to show an appropriate error message:

ControlToValidate="lname"

ErrorMessage="Last name is mandatory" />

As long as you’re using an up-level browser and client-side scripting is enabled for each validator, which is the default, invalid input will display error messages without performing a postback.

Important

Note that just tabbing through the controls is not a condition that raises an error; the validator gets involved only if you type blanks or if the field is blank when the page is posted back.

How can you determine whether a certain field is really empty? In many cases, the empty string is sufficient, but this is not a firm rule. The InitialValue property specifies the initial value of the input control. The validation fails only if the value of the control equals InitialValue upon losing focus. By default, InitialValue is initialized with the empty string.

Special Capabilities


The primary reason why you place validation controls on a Web form is to catch errors and inconsistencies in the user’s input. But how do you display error messages? Are you interested in client-side validation and, if you are, how would you set it up? Finally, what if you want to validate only a subset of controls when a given button is clicked? Some special capabilities of validation controls provide a valid answer to all these issues.

Server-Side Validation


Validation controls are server-side controls; subsequently, they kick in and give a response on the server. All postback controls (for example, buttons, auto-postback controls, and controls that registered as postback controls) validate the state of the page before proceeding with their postback action. For example, here’s how the Button control handles it. The Web Forms page life cycle ends up invoking the RaisePostBackEvent method to force the clicked submit button to execute its click handler:

// Code excerpted from the source code of the System.Web.UI.WebControls.Button

protected virtual void RaisePostBackEvent(string eventArgument)

{

base.ValidateEvent(this.UniqueID, eventArgument);

if (this.CausesValidation)

{

this.Page.Validate(this.ValidationGroup);

}

this.OnClick(EventArgs.Empty);

this.OnCommand(new CommandEventArgs(this.CommandName, this.CommandArgument));

}

The Validate method on the class Page just loops through the validators registered with the specified validation group and returns a response. The response simply updates the state of validation controls including the validation summary. This response will then be merged into the page response and output to the user.

If you simply need to know whether the state of the

Return Main Page Previous Page Next Page

®Online Book Reader