views:

35

answers:

2

I'm working on an application form for a website which implements ASP.NET validation (including client side).

I have a requirement to display a message at the bottom of the page if the client validation fails. Something along the lines of "Please go back and check your answers".

The problem is, the submit button's OnClientClick event obviously fires before the client validation.

Any idea how I can get around this problem?

+1  A: 

Have you set the CausesValidation property on your button? What is your OnClientClick method doing?

As I understand it you are trying to reinvent the wheel by implementing you own functionality to display the validation message. You should use a ValidationSummary control to display the message to the user. And you should also remove your custom OnClientClick function.

klausbyskov
Yes, I do, and my OnClientClick method is displaying the "Please go back..." message. The problem is that even if validation succeeds, this message is briefly seen before the form submits.
Liggi
@Liggi, I have updated my answer.
klausbyskov
I was attempting to do this, but I must be missing something. How do I set the ValidationSummary to just display some text, rather than giving a summary of all the errors? I literally just want it to display "Please go back and check your answers."
Liggi
@Liggi, I'm guessing that you have a bunch of `RequiredFieldValidators` or the like, right? If you set the `ErrorMessage` on those to an empty string, and set the `HeaderText` message on the `ValidationSummary` control to "Please ... ", I think you should be able to achieve what you want
klausbyskov
That's correct, unfortunately I'm also using the RequiredFieldValidators' error messages! So I can't do that.
Liggi
@Liggi, hmmm. How about using a css style to control the height of your validationsummary control so that it actually displays the messages, but they are not visible? It's pretty hacky :-(
klausbyskov
Looks like I solved the problem, just needed to set ErrorMessage to empty string and use Text instead for the other error messages. Thanks guys.
Liggi
+1  A: 

On your RequiredFieldValidator if you use both the ErrorMessage and Text attributes the value of ErrorMessage will appear in the ValidationSummary, while the value of Text will appear at the location of the RequiredFieldValidator.

E.g.:

<RequiredFieldValidator 
ErrorMessage="This will appear in the summary at the bottom of the page." 
Text="This will appear in the middle of the page." 
.../>

So to hide the ErrorMessage from the summary simply set it to an empty string:

ErrorMessage=""
Buh Buh