
March 2nd, 2008, 05:10 PM
|
 |
The Drunken Moderator
|
|
Join Date: Feb 2004
Location: Reston, VA, USA
|
|
|
ASP.Net/VB.Net - Programmatically Add Item to ValidationSummary
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.
VB.Net Code:
Original
- VB.Net 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:
VB.Net Code:
Original
- VB.Net Code |
|
|
|
Page.Validate() ValidationError.Display("Place error message here")
|