Online Book Reader

Home Category

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

By Root 5737 0
control lets you compare the value entered by the user with a constant value or the value specified in another control in the same naming container. The behavior of the control is characterized by the following additional properties:

ControlToCompare Represents the ID of the control to compare with the current user’s entry. You should avoid setting the ControlToCompare and ValueToCompare properties at the same time. They are considered mutually exclusive; if you set both, the ControlToCompare property takes precedence.

Operator Specifies the comparison operation to perform. The list of feasible operations is defined in the ValidationCompareOperator enumeration. The default operator is Equal; feasible operators are also LessThan, GreaterThan, and their variations. The DataTypeCheck operator is useful when you want to make sure that certain input data can be converted to a certain type. When the DataTypeCheck operator is specified, both ControlToCompare and ValueToCompare are ignored. In this case, the test is made on the type of the input data and succeeds if the specified data can be converted to the expected type. Supported types are expressed through the following keywords: String, Integer, Double, Date, and Currency (decimal).

ValueToCompare Indicates the value to compare the user’s input against. If the Type property is set, the ValueToCompare property must comply with it.

The following code demonstrates the typical markup of the CompareValidator control when the control is called to validate an integer input from a text box representing someone’s age:

ControlToValidate="ageTextBox"

ValueToCompare="18"

Operator="GreaterThanEqual"

Type="Integer"

ErrorMessage="Must specify an age greater than 17." />

The CustomValidator Control


The CustomValidator control is a generic and totally user-defined validator that uses custom validation logic to accomplish its task. You typically resort to this control when none of the other validators seems appropriate or, more simply, when you need to execute your own code in addition to that of the standard validators.

To set up a custom validator, you can indicate a client-side function through the ClientValidationFunction property. If client-side validation is disabled or not supported, simply omit this setting. Alternatively, or in addition to client validation, you can define some managed code to execute on the server. You do this by defining a handler for the ServerValidate event. The code will be executed when the page is posted back in response to a click on a button control. The following code snippet shows how to configure a custom validator to check the value of a text box against an array of feasible values:

ControlToValidate="membership"

ClientValidationFunction="CheckMembership"

OnServerValidate="ServerValidation"

ErrorMessage="Membership can be Normal, Silver, Gold, or Platinum." />

If specified, the client validation function takes a mandatory signature and looks like this:

function CheckMembership(source, arguments)

{

...

}

The source argument references the HTML tag that represents the validator control—usually, a tag. The arguments parameter references an object with two properties, IsValid and Value. The Value property is the value stored in the input control to be validated. The IsValid property must be set to false or true according to the result of the validation.

The CustomValidator control is not associated in all cases with a single input control in the current naming container. For this type of validator, setting the ControlToValidate property is not mandatory. For example, if the control has to validate the contents of multiple input fields, you simply do not set the ControlToValidate property and the arguments.Value variable evaluates to the empty string. In this case, you write the validation logic so that any needed values are dynamically retrieved. With client-side script code, this can be

Return Main Page Previous Page Next Page

®Online Book Reader