WebForms Validation Summary Use StartUpScript to Generate Error Message Box Without Using Client-Side Validation

Scenario

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

Solution

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

”’ Registers a javascript to display error messages from validation as the Page loads from a postback.
Private Sub RegisterServerValidationMessageScript()
If (Page.IsPostBack) Then
Page.Validate()
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
errorMessage.Append("\r\n")
End If
End If
End If
Next

errorMessage.Append("');”)
If Not Page.ClientScript.IsStartupScriptRegistered(“validationAlert”) Then
Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), _
“validationAlert”, errorMessage.ToString(), False)
End If
End If
End If
End Sub

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.

Advertisements

Tags: , ,

2 Responses to “WebForms Validation Summary Use StartUpScript to Generate Error Message Box Without Using Client-Side Validation”

  1. jeffhardy Says:

    Hey,

    Is this the right way to follow. As there is no client recommendation so is it will prove fruitful? I am very confused whether to use this technique.

    I like the idea behind this still not getting exactly to use it or not? Did the client will give the approval for this? Looking for the perfect advice.

    Thanks in advance.

    • baraholka1 Says:

      Hi Jeff,

      In retrospect I would not use this technique again.
      Much better to improve one’s Javascript or JQuery skills and do it via that setting in the ValidationSummary Control that forces the ValidationSummary to emit Javascript.
      No, the client did not approve my use of this technique, though they have not yet complained either.
      The app is an internal app used only once per month to update data for end of month specials.
      So they’re living with whatever annoyance I subjected them to.
      But its not so bad in the context of this particular app.

      Thanks for dropping by and be sure to BTWT again.

      Regards,

      Barra

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: