views:

3407

answers:

9

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.

A: 

Did you call Update on your updatepanel?

Per Hornshøj-Schierbeck
I am not using update. The updatepanel is used for postbacks only.
Abdu
A: 

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.

Jorge Alves
+1  A: 

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

Euro Micelli
+1  A: 

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?

Abdu
Well, if you want to see the validation results on the client side the updatepanel where the validators reside **needs** to be updated. Otherwise, the error messages won't appear.What's the result of Page.IsValid() in the control events you're using?
Jorge Alves
Can you clarify what you mean by "update" when you say "I don't want to force an update"? If you are issuing a postback, UpdatePanel or not, you are going to get a page update, even if it might not look that way to the user.
Euro Micelli
I meant I don't want to use the panel's Update(). I am expecting the postback to trigger the validators. The validators are inside the updatepanel. All the server controls are inside the update panel. The IsValid is true when it shouldn't because when I click on the submit button, I see messages.
Abdu
+1  A: 

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.

Euro Micelli
A: 

1- I am using 3.5 SP1.
2- Yes. The whole page goes like this:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  <asp:UpdatePanel ID="upForm" runat="server">
    <ContentTemplate> 
... some validation controls.....
... rest of page ....
 </ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

3- JS is enabled

Abdu
After your clarifications, the only thing I can think of is a validation group mismatch.
Jorge Alves
A: 

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.

Abdu
A: 

Is there solution for Client side validation?

I have the same situation. In my case I've 2 UpdatePanels and some validators(Range, Reaquired, Custom...).

I have done the test project with Required validator - it works fine.

A: 

Is there solution for Client side validation?

I have the same situation. In my case I've 2 UpdatePanels and some validators(Range, Reaquired, Custom...).

I have done the test project with Required validator - it works fine.

I has solved my problem. It was in url rewriter.

Thanks.