CustomValidator

L

Louis Yeung

Hi,

I am playing with validators using VS2003 on a XP box. I have textboxes with
regExp validators and a button. The validators work when I press the button.
If any validator fails, error messages are displayed and button click event
does not fire (i.e. not hitting my breakpoint). Everything works as stated.
Some error messages are in line and some are shown in a validationSummary
control.

I added a customValidator to a textbox which already has a regExp
validator. The custom validation code was fired. I set args.IsValid=false. I
was surprised the button click event was fired. Inside the event handler, I
examined the customValidator.IsValid and WebForm1.IsValid values which are
both false.

According to MSDN , the customValidator should behave like other validators.
Can someone explain why the button event is fired eventhough the
customValidator fails the validation.

...Louis
 
B

Brock Allen

All server events fire, regardless of the outcome of Validation. In your
button handler, though, you should be checking Page.IsValid prior to making
assumptions about the data the user's provided. IOW, check Page.IsValid before
you use the data and save it to your database.

It sounds like you're using a CustomValidator without a client side validation
routine (which is fine). This is why your request made it to the server.
You can also provide a client side javascript validation function if that's
useful to you.

But again, you should always check IsValid in your event handlers since the
user can disbale javascript in the browser.
 
L

Louis Yeung

Brock,

I agree with you after I read another MSDN article. Why my button click
event did not fire when my regExp validator failed? I checked the displayed
page content and do not believe the regExp validator is running on client.

...Louis
 
B

Brock Allen

To disable the client-side script for testing purposes only, add ClientTarget="downlevel"
to your @Page directive and test it again to see if it really calls the button
click event:

<%@ Page ClientTarget="downlevel" ... %>
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top