2009-01-12 55 views
0

我有一個頁面嘗試登錄用戶。在頁面的頂部,我有一個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 

回答

2

我想你想設置的ErrorMessage屬性,甚則Text屬性。

+0

我想我一直在看代碼太長。謝謝 – 2009-01-12 19:38:31