Why is my ASP.NET CustomValidator not checking?

I have to do something wrong. It seems I can not execute my CustomValidator ServerValidate method.

I have an ASP.NET Visual Basic page with 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 have a validation set that always fails ...

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 is never executed!

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

...

CustomValidator1.Validate() 'does nothing?

?

+3
4

:

 ValidateEmptyText="True"
+9

?

+2

, ( !). ? , .

+1

-, , , OnServerValidate .

Secondly, I would check that CustomValidator1_ServerValidate was configured as an event handler for the ServerValidate event for Textbox1. I had cases when I changed the name of the markup verification method and code, but in the IDE the name of the subscription method passed to the event handler delegate was not automatically updated

+1
source

Source: https://habr.com/ru/post/1702273/


All Articles