C# – CustomValidator OnServerValidate Not Working

ccode-behindcustomvalidatortextbox

I am trying to utilize a CustomValidator using the keyword "OnServerValidate"

Here is where I have it setup in my .aspx file:

<div class="label">
    <div><label for="parentemail1">Email Address</label></div>
    <div><asp:TextBox ID="parentemail1" runat="server" MaxLength="40" Columns="40"></asp:TextBox></div>                      
        <asp:CustomValidator id="ParentEmail1Required"
        ControlToValidate="parentemail1"
        Display="Dynamic"
        ErrorMessage="Required"
        ValidateEmptyText="true"
        OnServerValidate="ServerValidator"
        runat="server"/>                          
</div>

and here is the C# code behind method for OnServerValidate:

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

Am I missing something, shouldn't the CustomValidator fire when the form is submitted?
I've read through a bunch of posts and even tried this with a RequiredFieldValidator on the same control but no luck. I have this working with ClientValidationFunction but I want to access the properties in the code behind instead of the DOM.

Any help would be greatly appreciated.

Best Answer

Try this

protected void ServerValidator(object source, ServerValidateEventArgs args)
{
    if(args.IsValid == false)
       return;
    Response.Redirect("NextPage.aspx");// It will not fire if page is not valid.
}