I wanted to use ValidationSummary.ShowMessageBox to show a popup containing the error message from all failed validations but I had some CustomValidator controls and didn’t want to write complex client-side validation function for each one of them.
I needed a way to get the ErrorMessages from each CustomValidator and all my other Validators into a MessageBox without writing any custom client-side validation routines
The solution is to collect all the error messages from the Validators into a string and use ClientScript.RegisterStartUpScript to show the errors. This solution comes via a semi-genius post from the well-known Missourian Programming legend bdukes. All I have done is translate it into VB.NET. Here’s the C# original from Stack Overflow thread Elegant way to make CustomValidator work with ValidationSummary Messagebox
Private Sub RegisterServerValidationMessageScript()
If (Page.IsPostBack) Then
If (Not Page.IsValid) Then
‘errormessage holds the script
Dim errorMessage As StringBuilder = _
New StringBuilder(“alert(‘Validation Error. \r\n\r\n”)
Dim i As Integer
‘ Add error message from all failed validators
For i = 0 To Page.Validators.Count – 1
Dim validator As IValidator = Page.Validators(i)
If (Not validator.IsValid) Then
If Not errorMessage.ToString().Contains(validator.ErrorMessage) Then
errorMessage.Append(“- ” + validator.ErrorMessage)
If (i < Page.Validators.Count – 1) Then
If Not Page.ClientScript.IsStartupScriptRegistered(“validationAlert”) Then
“validationAlert”, errorMessage.ToString(), False)
I just call the above function from Page_PreRender Event Handler.
Almost No Known Side Effects
The side-effect to the above code is that it is called on every Postback, so you will get the alert popup appearing on every Postback while you have validation errors. But what I like about it is that any time I add a new Validator, its error message is automagically added to the alert popup. So that’s less maintenance for me.
Hope that rings your bells.