Sanya Zahid Sanya Zahid - 7 months ago 38
Vb.net Question

Custom Validator not working (VB .net)

Here is the custom validator code

<asp:CustomValidator ID="ModelValidator" runat="server"
ErrorMessage="Model is required."
ForeColor="Red" ValidateEmptyText="true"
OnServerValidate="Model_Validate" ></asp:CustomValidator>


Code Behind

Protected Sub Model_Validate(source As Object, args As ServerValidateEventArgs) Handles ModelValidator.ServerValidate
Dim value As String = txtModel.SelectedItem.Text
If value = "--None--" Then
args.IsValid = False
End If

End Sub


It is running fine but not showing the error
enter image description here

Answer

You should check for page.IsValid as below:

Protected Sub Model_Validate(source As Object, args As ServerValidateEventArgs) Handles ModelValidator.ServerValidate
If Page.IsValid Then
Dim value As String = txtModel.SelectedItem.Text
            If value = "--None--" Then
                args.IsValid = False
            End If
End If           
End Sub