R – Why won’t the ASP.NET CustomValidator validate

asp.netcustomvalidatorvb.net

I must be doing something wrong. I can't seem to execute my CustomValidator's ServerValidate method.

I've got a Visual Basic ASP.NET page with a CustomValidator…

<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
    ControlToValidate="TextBox1"
    ErrorMessage="Friendly message goes here."
    Display="Dynamic" />
<asp:Button ID="Button1" runat="server"
    Text="Submit"
    CausesValidation="True" />

For this test, I've got the validation set to always fail…

Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
    args.IsValid = False
End Sub

But, when the button is clicked, the CustomValidator1_ServerValidate() method never executes!

Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Page.Validate()
    If Page.IsValid Then
        'it executes the code here!
    End If
End Sub

Not even if I explicitly validate that control…

CustomValidator1.Validate() 'does nothing?

What am I doing wrong?

Best Solution

Add the property:

 ValidateEmptyText="True"
Related Question