自定义验证程序无法正常工作(VB .net)

时间:2016-04-28 04:52:35

标签: vb.net

以下是自定义验证码

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

背后的代码

 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

运行正常,但未显示错误 enter image description here

1 个答案:

答案 0 :(得分:1)

您应该检查page.IsValid,如下所示:

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