2016-04-28 71 views
1

下面是自定義的驗證碼定製驗證不工作(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

在你身後應檢查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 
+0

做了那個已經還沒有拋出任何錯誤的 –

+0

你設置了控件的屬性來驗證嗎? –

+0

我已經做到了,謝謝 –