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.
- The
ValidationGroupproperty of both the button and theCustomValidatorare the same - 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" />
Try to force the validation in the button-click handler via
Page.Validate:Edit(from comments):
If you want the customvalidator to validate if nothing was entered/selected in your controls, you need to set
ValidateEmptyTextto true. You also might want to let theCustomValidatorreplace theRequiredFieldValidators.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 theSeverValidateto be costlier than a simple text-is-empty check.