Abdu
Abdu

Reputation: 16585

Do standard asp.net validators work with Ajax and update panel?

I am having issues with validators not firing (No expected error messages showing) when using Page.Validate() from code behind. The validators are placed inside an Ajax updatepanel.

It seems there are downloadable Ajax versions of the validators. I am not sure if I need these or if VS 2008 SP1 has them already. When the form is posted through a button, the validators work but they don't when I do a Page.Validate() on demand.

Upvotes: 1

Views: 6557

Answers (6)

Abdu
Abdu

Reputation: 16585

I ended up using a single custom validator and doing my own validations in code behind and setting the custom validator's error message. This way I had more flexibility and it worked. Using Ajax, it feels like client side validation.

Upvotes: 0

Euro Micelli
Euro Micelli

Reputation: 34018

Maybe we can take it from the top. Can you answer these?

  • Are you using .NET 2.0 SP1 or greater?
  • Are your validator controls inside the UpdatePanel or outside?
  • Are you using your site with javascript disabled (very unlikely)?

Note that your validators MUST be inside an updated UpdatePanel for them to display the error messages. If they are not in an updated UpdatePanel, the validators cannot change their appearance on the browser.

Upvotes: 1

Abdu
Abdu

Reputation: 16585

I don't want to force an update. In certain situations, I want to validate some form elements when a user changes the value on some form element. When I user makes a change to say a radio button or a dropdownlist, an automatic postback happens. When the postback occurs, I want the validation controls to fire as if I hit the submit button.

These controls which cause a postback have 'causevalidation' turned on. Another test is in the event handler of the control which caused the postback, I have a Page.Validate().

The question is why a button postback fires the validation but not another control which caused a postback?

Upvotes: 1

Euro Micelli
Euro Micelli

Reputation: 34018

Yes, validators do work inside an UpdatePanel, but you need to use at least SP1 of ASP.NET 2.0. If you use SP1, you do not need and should not use the "ajax version" of the validators.

More details on this subject are available here:

StackOverflow: ASP.NET Validators inside an UpdatePanel

Upvotes: 1

Jorge Alves
Jorge Alves

Reputation: 1168

They were included in a update for .Net framework a while ago, so yes, you have them in VS2008 SP1. I've found a problem where the server side method for CustomValidators fires twice with no "evil" effect, but otherwise they work ok.

As for the specific problem you're having, maybe the validators aren't inside the updatepanel, or some other panel ends up being refreshed by whatever control posted instead of the one that you want? Or even some ValidationGroups are defined somewhere and only these end up being validated? It's very hard to say without seeing code.

But making sure your validators are shown is easy: MyUpdatePanel.Update() will force the refresh.

Upvotes: 0

Did you call Update on your updatepanel?

Upvotes: 0

Related Questions