CustomValidator ServerValidate method does not fire
Asked Answered
V

3

12

I've put a CustomValidator on my form. I have not set its ControlToValidate property. In its ServerValidate event I've written the following:

protected void CustomValidator1_ServerValidate(object source,      
                                               ServerValidateEventArgs args)
{
    args.IsValid = false;
}

I put a breakpoint to this method but it seems to never come to that point. But if I do this on another form it works like a charm.

  1. The ValidationGroup property of both the button and the CustomValidator are the same
  2. I tried deleting this property in both the button and the CustomValidator, still does not work.

It seems as there's something formwide. I just put a CustomValidator on the form and do not touch any of its properties other than just setting its ServerValidate event method.

EDIT: Here's the aspx part:

 <asp:CustomValidator ID="CustomValidator2" runat="server" 
       ErrorMessage="This is a test" 
   onservervalidate="CustomValidator1_ServerValidate" 
   ValidationGroup="PA"></asp:CustomValidator>


<asp:Button ID="btnPensionersOK" runat="server" Text="OK" Width="75px" 
          onclick="Button1_Click" ValidationGroup="PA" />
Vasta answered 16/4, 2012 at 10:27 Comment(0)
D
28

Try to force the validation in the button-click handler via Page.Validate:

protected void Button1_Click(Object sender, EventArgs e)
{
    Page.Validate();
    if(Page.IsValid)
    {
       // servervalidate should have been called
    }
}

Edit(from comments):

If you want the customvalidator to validate if nothing was entered/selected in your controls, you need to set ValidateEmptyText to true. You also might want to let the CustomValidator replace the RequiredFieldValidators.

I assume that the validator-order on the aspx decides whether or not a customvalidator's severvalidate is called if a previous Validator already has made Page.IsValid=false. Or ASP.NET is so smart that it assumes the SeverValidate to be costlier than a simple text-is-empty check.

Drag answered 16/4, 2012 at 10:43 Comment(10)
But how come other validators work? I have several RequiredFieldValidator on the same form but they work normally. And again, I do exactly the same thing on another form, and there's no problem, it validates. The things is ServerValidate method does not fire.Vasta
@MikeJM: I don't know. Are you databinding in page_load on postbacks? Are you calling somewhere else Page.Validate()? Do you get any javascript errors? Do the RequiredFieldValidators prevent the postback? Is it posting back at all?Drag
Yes, I am databinding in page_load but not on PostBack. I never call Page.validate() anywhere else.I do not get any javascript errors.The RequiredFieldValidators prevent postback. It is posting back.Vasta
@MikeJM: Have you checked whether or not ServerValidate is firing on Page.Validate()?Drag
OK, look what I got. I changed ValidationGroup property of both the Button and the customValidator. In my button's click event I added Page.Validate(); Then I stepped into that mehod, it went directly to the ServerValidate method of the CustomValidator. But, I still received one of the RequiredFiledvalidators' error message.It seems as that RequiredFieldValidator preventing CustomValidator.Vasta
@MikeJM: Yes of yourse, that's why i've asked for the RequiredFieldValidators. If you want the customvalidator to validate if nothing was entered/selected in your controls, you need to set ValidateEmptyText to true. You also might want to let the customvalidator replace the RequiredFieldValidators. But if anything was entered/selected, the ServerValidate should fire, doesn't it? I assume that the validator-order on the aspx decide whether or not a customvalidator's severvalidate is called if a "previous" Validator already has made Page.IsValid=false.Drag
Thank you Tim so much. You opened my eyes to something that I was not aware of. It worked after setting that property to true. I wish i could vote you up by a million, I'm sure you don't need it though.Vasta
Wow.. what an unclear property haha a chance ive seen this post otherwise i couldnt have found it. Thanks guys!Lasonyalasorella
i am also facing this while uploading customvalidator to validate if nothing was entered/selected in your controls i was just put ValidateEmptyText is true and working like as charms.Readymix
<asp:CustomValidator ID="CV_FUp_Front" runat="server" ErrorMessage="Max size 100KB. Upload only JPEG/JPG/PNG format" ValidateEmptyText="true" ClientValidationFunction="ValidateFile" OnServerValidate="ValidateFile_FUp_Front" Display="Dynamic" ValidationGroup="Submit" ControlToValidate="FUp_Front" Font-Size="8pt" CssClass="error" />Readymix
V
1

I would also like to put some more help for those who will use CustomValidators and RequiredFieldValidators at the same time. One should take into account that Client side validation takes place first. And the server side validation will occur only after PostBack. I'm sure you got it but just in case this is not quite clear: It means first all the controls that are bound to certain client side working validators must be valid to let Postback to occur. After Page. IsValid is True server side stuff takes place and posts back any changes which includes server side validation messages.

So here are the ways one can make both CustomVCalidators and other built in validators to work at the same time.:

  1. Set both groups of validators to work on Client side. In this case we must ensure that for the custom valitor(s) we spacify the script that will make validation on the client side. Without writing script and just filling in the ServerValidate method the validation will take place in the server.Even if EnableClientScript property is set to True.

  2. Set both groups of validators to work on server side. To do so simply set EnableClientScript to False. But note that this will load the server.

Vasta answered 16/4, 2012 at 13:9 Comment(0)
R
1

Adding upon existing answers:

If the validated control is placed inside a control which is not visible (for example, if it's placed in an asp:Panel that was set to Visible = false), the validation won't be triggered.

To resolve, we can momentarily make it visible, for example:

// assuming SomeUpdatePanel contains SomePanel
SomePanel.Visible = true;
SomeUpdatePanel.Update(); 
Page.Validate();
SomePanel.Visible = false;
SomeUpdatePanel.Update();
Rech answered 24/10, 2023 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.