CustomValidator ServerValidate Method Fails

I put CustomValidator in my form. I have not set the ControlToValidate property. In my ServerValidate event, I wrote the following:

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

I set a breakpoint on this method, but it doesn't seem to be happening. But if I do it in a different form, it works like a charm.

  • The ValidationGroup property of both the button and CustomValidator same
  • I tried to remove this property both in the button and in CustomValidator , it still does not work.

There seems to be something in common. I just put CustomValidator on the form and do not touch any of its properties, except how to simply set my ServerValidate event ServerValidate .

EDIT: here is 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" /> 
+8
c # customvalidator
source share
2 answers

Try forcing a check in the button handler using 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 customvalidator to confirm that nothing has been entered / selected in your control, you need to set ValidateEmptyText to true. You can also allow CustomValidator to replace RequiredFieldValidators .

I assume that the aspx order validator decides whether the calling element is called nodevalidate or not, if the previous Validator already did Page.IsValid=false . Or ASP.NET is so smart that it suggests that SeverValidate will be more expensive than a simple error-free text check.

+19
source share

I would also like to provide additional assistance to those who will use CustomValidators and RequiredFieldValidators at the same time. Please note that the client-side check is performed first. And server-side validation will only happen after PostBack. I’m sure that you received it, but just in case, this is not entirely clear: this means that at first all controls bound to specific working validators on the client side must be valid to enable Postback. After the page. IsValid is the true server material that takes place and returns any changes that include server-side validation messages.

So, here is how you can make both CustomVCalidators and other built-in validators work.

  • Set both groups of validators to work on the client side. In this case, we need to make sure that for custom valitor (s) we will split the script, which will do client-side validation. Without writing a script and just populating the ServerValidate method, the check will be performed on the server. Even if the EnableClientScript property is set to True.

  • Set both groups of validators to work on the server side. To do this, simply set EnableClientScript to False. But keep in mind that this will load the server.

0
source share

All Articles