all groups > asp.net webcontrols > march 2005 >
You're in the

asp.net webcontrols

group:

CustomValidator


CustomValidator Louis Yeung
3/31/2005 8:07:04 AM
asp.net webcontrols:
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.

Re: CustomValidator Brock Allen
3/31/2005 8:12:18 AM
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.

-Brock
DevelopMentor
http://staff.develop.com/ballen



[quoted text, click to view]


Re: CustomValidator Louis Yeung
3/31/2005 8:57:03 AM
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

[quoted text, click to view]
Re: CustomValidator Brock Allen
3/31/2005 9:49:21 AM
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" ... %>

-Brock
DevelopMentor
http://staff.develop.com/ballen



[quoted text, click to view]


Re: CustomValidator Peter Blum
3/31/2005 12:51:08 PM
The reason it behaves this way is that you didn't supply a client-side
validation function for your custom validator. Client-side validation
processes the regular expression validator and thinks the page is fine. It
then submits the page. At that point, server side validation is run and
calls all validators including your customvalidator. By design, this step
happens after Page_Load and just before calling your Click event handler
method. Inside your Click event handler you should *ALWAYS* check the state
of validation on Page.IsValid because Click is *always* called even if the
page is invalid.

--- Peter Blum
www.PeterBlum.com
Email: PLBlum@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

[quoted text, click to view]

AddThis Social Bookmark Button