我有一个试图登录用户的页面。在页面顶部,我有一个ValidationSummary控件。我没有在页面中显式声明的控件,并且我正在调用一个静态方法,以便在出错时将Validator添加到页面中。 (见下文)
提交页面时,将显示ValidationSummary,但ValidationSummary控件中不会显示任何错误消息。它几乎就像控件不知道要写入控件的错误文本。
我是否必须覆盖BaseValidator中的方法才能显示验证程序的错误文本?
以下是验证器添加到页面的方式:
Private Sub btnWindowsLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnWindowsLogin.Click
Dim username As String = txtNetworkID.Text.Trim
Dim password As String = txtPassword.Text
If username.IsEmpty Then
ErrorSummary.AddError("Please enter your NT Login", "WindowsLogin", Page)
End If
If password.IsEmpty Then
ErrorSummary.AddError("Please enter your password", "WindowsLogin", Page)
End If
If Page.IsValid Then
If Not AuthenticationService.ValidateActiveDirectoryLogin(username, password) Then
ErrorSummary.AddError("The username or password you entered is incorrect", Page)
ElseIf Not UserService.WindowsLoginExists(username) Then
ErrorSummary.AddError("The NT Login entered is not associated with an account in the application", Page)
Else
'Get the user and validate the role, if the user is active, etc...
End If
End If
End Sub
这是ErrorSummary类:
Public Class ErrorSummary
Inherits BaseValidator
Public Sub New(ByVal message As String, ByVal validationGroup As String)
MyBase.Text = message
MyBase.ValidationGroup = validationGroup
MyBase.IsValid = False
End Sub
Public Sub New(ByVal message As String)
Me.New(message, String.Empty)
End Sub
Public Shared Sub AddError(ByVal message As String, ByVal page As Page)
AddError(message, String.Empty, page)
End Sub
Public Shared Sub AddError(ByVal message As String, ByVal validationgroup As String, ByVal page As Page)
Dim objError As New ErrorSummary(message, validationgroup)
page.Validators.Add(objError)
End Sub
Protected Overrides Function EvaluateIsValid() As Boolean
Return False
End Function
End Class