how do I make the validator cut off other code on the page from executing?

B

bennett

At
http://www.brainjammer.com/testing/validator_test.aspx
I have a text field where you can enter text, and a button where if you
click the button, it sets the value of a label below it, to display the
text you entered in a text field. There is also a
RequiredFieldValidator for the text field so that you get an error if
you leave it blank.

As long as I have EnableClientScript set to true for the
RequiredFieldValidator, if I leave the text field blank and click the
button (ignore what the button caption says, "hide this panel" etc.),
the RequiredFieldValidator shows, and the text of the label below it
does not get set to blank. i.e., when the validator detects an error
in the field, it stops the other button click handling code from
running, which is what you'd expect.

The problem is that if EnableClientScript for the validator is set to
false (which is how I have it set now), then when you enter blank for
the text field and click the button, the validator still displays,
*but* the text of the label below it also changes to blank. So when
the validator is set to only do server-side validation, it doesn't cut
off the other button click handling code from executing when it detects
an error.

Is there any way to make the validator stop the button's onclick event
code from running if the validator detects an error in the field that's
submitted?

I can just set EnableClientScript to true, but some of our users have
JavaScript turned off in their browser, which appears to have the same
effect as having EnableClientScript set to false on the ASP page -- the
button's onclick event handler runs to completion even if the validator
detects an error in the field.

Of course instead of using a validator, I could just put some code in
the button's onclick event handler that validates the control myself,
and displays the error and exits if something's wrong, but it's more
elegant to use the built-in validators if possible.
 
P

Peter Blum

I think you are asking how to test if the page is invalid before
saving/redirecting, etc.

*Always* test Page.IsValid is true in your Click event method before saving.
Its not done automatically:

Protected Sub Submit_Click(parameters)
If Page.IsValid Then
' your code to save/redirect, etc
End If
End Sub

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top