Programming Microsoft ASP.NET 4 - Dino Esposito [206]
Note
The Validate method on class Page is always invoked during the postback stage, regardless of the features of the postback control—a submit button has different postback mechanics compared to, say, a link button or an auto-postback control. In general, you’ll more likely need to call IsValid in the code-behind class than Validate. After validation has occurred, in fact, you might need to check whether it was successful before you perform some other operations.
Displaying Error Information
The ErrorMessage property determines the static message that each validation control will display if an error occurs. You need to know that if the Text property is also set, it will take precedence over ErrorMessage. Text is designed to display inline where the validation control is located; ErrorMessage is designed to display in the validation summary. (Strategies for using Text and ErrorMessage will be discussed more in the next section, “The ValidationSummary Control.”) Because all validation controls are labels, no other support or helper controls are needed to display any message. The message will be displayed in the body of the validation controls and, subsequently, wherever the validation control is actually placed. The error message is displayed as HTML, so it can contain any HTML formatting attribute.
Validators that work in client mode can create the tag for the message either statically or dynamically. You can control this setting by using the Display property of the validator. When the display mode is set to Static (the default), the element is given the following style: style="visibility:hidden;" The CSS visibility style attribute, when set to Hidden, causes the browser not to display the element but reserves space for it. If the Display property contains Dynamic, the style string changes as follows: style="display:none;" The CSS display attribute, when set to none, simply hides the element, which will take up space on the page only if displayed. The value of the Display property becomes critical when you have multiple validators associated with the same input control. (See Figure 9-4.) Figure 9-4. Input controls in the form are validated on the client. As you can see, the Hire Date text box is first validated to ensure it contains a valid date and then to verify the specified date is later than 1-1-1999. If the Display property is set to Static for the first validator, and the date is outside the specified range, you get a page like the one shown in Figure 9-5. Figure 9-5. Static error messages take up space even if they’re not displayed. Multiple Validators per Control Text="!!!" ErrorMessage="Name is mandatory" /> Text="!!!" ErrorMessage="Last name is mandatory" /> Operator="GreaterThanEqual" ValueToCompare="18" Type="integer" ErrorMessage="Age must be at least 18." />
Note that you can associate multiple validators with a single input control. Here’s an excerpt from the code behind the page in Figure 9-5:Name * Last Name * Age Email ®Online Book Reader