Controlling the display of ASP.NET validator controls

Recently, I worked on an old webform project and used the asp.net validation controls. I previously used some features of validation controls, but I encountered more scenarios this time. I found these controls are fairly flexible and can be configured to work in many different ways:

Validator Controls

The most important properties that control the display are Text, ErrorMessage and Display properties.

What is the difference between the Text and ErrorMessage properties. According to MSDN:

If the Text property is set, that text will override the text specified in the ErrorMessage property and appear in the validation control. However, the text specified by the ErrorMessage property always appears in the ValidationSummary control.

One scenario is that you might set Text to “*” and ErrorMessage to the description of the error. You will see an “*” next to the control to validate and a details message in the validation summary section.

The display property takes 3 values:

Display Behavior Description
None
The validation message is never displayed inline.
Static Space for the validation message is allocated in the page layout.
Dynamic Space for the validation message is dynamically added to the page if validation fails.

To show error only in the validation summary section, set the Display property of the validator controls to None.

Validation Summary Controls

The most important properties that control the display are HeaderText, DisplayMode, ShowSummary and ShowMessageBox properties.

If we turn Show Summary off and ShowMessageBox on, the summary will be displayed as a message box. Note that it is necessary to leave EnableClientScript to true to use ShowMessageBox.

Supposing we only want to display a “*” next to each control and a generic message portion, we only need to test Text of each validator control to “*”, ErrorMessage to blank and set the generic message to the HeaderText of the ValidationSummary control.

The ASP.NET validator controls have a good combination of properties to work out all the scenario that I encountered in the project.

No Comments