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


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"
    ErrorMessage="Friendly message goes here."
    Display="Dynamic" />
<asp:Button ID="Button1" runat="server"
    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
    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:

Related Question