Asp – Problem rendering error text from Custom Validator

asp.netvalidation

I have a page that is trying to login a user. at the top of the page, I have a ValidationSummary control. I don't have the controls explicitly declared in the page and am calling a static method to add the Validator to the page on an error. (see below)

When the page is submitted, the ValidationSummary appears, however, none of the error messages in the ValidationSummary control display. It's almost like the control doesn't know what error text to write to the control.

Do I have to override a method in the BaseValidator in order to display the validator's error text?

Here is how the validator is added to the page:

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

and here is the ErrorSummary class:

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

Best Answer

I think you want to set the ErrorMessage property, rather then the Text property.

Related Topic