In this post I will to explain you how you can use the validator summary control of the asp.net. You have used required field validator, regular expression validator range validator etc, which are used to display the message for the controls, depend on the location of the control where they are placed. I think it is better to display all the message on same location so that user can read them and correct the error for the input. So you can use the validator summary control to display all or some of the message on the same place so that user can read all the message at same place. Here is the simple main screen of the example which is used for the validator summary control
In the above screen you can see that I have only two text box control and two button control. So I have two required field validator control one for the user login name and second one for the password, So if you didn't enter any thing in any of these text box then the message will be displayed. Last and the important control is the validator summary control which is used to display the both the message at top of the controls. Let us start with the validator summary control what properties are offer by the control and how you can use them.
The first Property is the display mode of the validation summary. Here is the list of mode which you can use to display the summary of the message.
Member name | Description | ||
---|---|---|---|
List | Validation summary displayed in a list. | ||
BulletList |
| ||
SingleParagraph | Validation summary displayed in a single paragraph. |
For this example I have set the display mode of the validator summary to be bullet list mode. Next interest property of the validator summary control is the ShowMessageBox.This property can be used in addition to the ShowSummary property to control where the validation summary is displayed. If this property and EnableClientScript are both set to true, the validation summary is displayed in a message box. If EnableClientScript is set to false, this property has no effect. When you set the showMessageBox property of the validator control to true, the message layout depend on the display mode property of the validator control. If you set the list to the display mode then the message shown in the in the alert dialog is in the form of list and so on for the ButtletList and SingleParagraph enum constant.
Now next is to set the properties of the other validator control so that there message can be displayed on the validator summary control.The error message displayed in the ValidationSummary control for each validation control on the page is specified by the ErrorMessage property of each validation control. If the ErrorMessage property of the validation control is not set, no error message is displayed in the ValidationSummary control for that validation control. You can also specify a custom title in the heading section of the ValidationSummary control by setting the HeaderText property.
Here is the output of the page when user didn't enter any thing in any of the Text box control. You can also see that I have set the header text of the validator control.
One last point about the validator summary control is that at the beginning of this article I have said that you can display all or sum of the message on same page to validator summary control, by using the ValidationGroup property of the validator control. If you set this property and give name to the validationgroup property then the validator control will only display the messages of the validator which have same validation group name. And you have to set the validation group property of the button control as well if you want to validate the controls. In my example I didn't set the validation group name property , but I have set the causeValidation property of the cancel button to false which means when cancel button is press no control validation occures. But if you set the causeValidation property of the cancel button to true ,and then you press the cancel button the validation occures and validation summary will be displayed.You can download the source code from here.
All and any comments / bugs / suggestions are welcomed!
No comments:
Post a Comment