I had an instance where I wanted to simply set off
ASP.Net's ValidationSummary control to display an error to the user instead of creating another element to handle this. After some research, I came across the IValidator class.
Code:
Imports System.Web.UI
Public Class ValidationError
Implements IValidator
Private _errorMessage As String = String.Empty
Private _isValid As Boolean = False
Public Shared Sub Display(ByVal message As String)
Dim currentPage As Page = TryCast(HttpContext.Current.Handler, Page)
currentPage.Validators.Add(New ValidationError(message))
End Sub
Public Sub New(ByVal message As String)
ErrorMessage = message
IsValid = False
End Sub
Public Property ErrorMessage() As String Implements System.Web.UI.IValidator.ErrorMessage
Get
Return _errorMessage
End Get
Set(ByVal value As String)
_errorMessage = value
End Set
End Property
Public Property IsValid() As Boolean Implements System.Web.UI.IValidator.IsValid
Get
Return _isValid
End Get
Set(ByVal value As Boolean)
_isValid = value
End Set
End Property
Public Sub Validate() Implements System.Web.UI.IValidator.Validate
End Sub
End Class
To call this, simply do the following:
Code:
Page.Validate()
ValidationError.Display("Place error message here")