+ Reply to Thread
Results 1 to 4 of 4

Thread: Programmatically Add Item to Validation Summary

  1. #1
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Programmatically Add Item to Validation Summary

    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")
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  2. #2
    Barn Newbie janets20 is an unknown quantity at this point janets20's Avatar
    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    2

    Hi

    Hi,

    I am showing messages using MessageBox (ShowMessageBox="True").Using your code snippet how can i show a MessageBox.


    Regards
    Janet

  3. #3
    Barn Newbie bermorg is an unknown quantity at this point bermorg's Avatar
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    2

    Hi,

    here is a simpler solution. Create a control that derives from BaseValidator. Implement the abstract EvaluateIsValid method. Make sure that the ControlPropertiesValid() method always returns true because there is no control to validate and set Display to ValidatorDisplay.None to prevent this control from displaying error text at its position in the HTML code.

    Here is the complete C# code:
    Code:
        [ToolboxData("<{0}:ValidationError runat=server />")]
        public class ValidationError : BaseValidator
        {
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
    
                ErrorMessage = ""; // Clear error message
                /*
                 * Prevent this control from displaying error text.
                 * This will be done in a ValidationSummary control.
                 */
                base.Display = ValidatorDisplay.None;
            }
    
            protected override void OnPreRender(EventArgs e)
            {
                /*
                 * Make the control check the ErrorMessage
                 */
                EvaluateIsValid(); 
                base.OnPreRender(e);
            }
    
            protected override bool ControlPropertiesValid()
            {
                /*
                 * This is always true because no control should be checked.
                 */
                return true; 
            }
    
            protected override bool EvaluateIsValid()
            {
                /*
                 * Invalid if any not empty error message has been set.
                 */
                base.IsValid = (ErrorMessage == null) || (ErrorMessage == "");
                return base.IsValid;
            }
        }
    
    To call this control, put it onto the page, and set theErrorMessage property in the code if required:
    Code:
                try
                {
                    ...
                    ...
                    ...
                }
                catch (Exception ex)
                {
                    ValidationError.ErrorMessage = ex.Message;
                }
    
    Last edited by bermorg; November 5th, 2010 at 02:09 PM.

  4. #4
    Barn Newbie slightstk is an unknown quantity at this point slightstk's Avatar
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    2

    Great solution

    I spent about 3 hours tyring to implement other variations to this problem but none worked.

    Thanks for posting this up it was a life saver.

+ Reply to Thread

Similar Threads

  1. validation
    By todd2006 in forum JavaScript Programming
    Replies: 5
    Last Post: July 30th, 2008, 10:23 AM
  2. How do you add a static item to a dynamic dropdownlist
    By Shem in forum .NET Development
    Replies: 19
    Last Post: July 4th, 2008, 09:14 AM
  3. textbox validation
    By todd2006 in forum JavaScript Programming
    Replies: 6
    Last Post: July 2nd, 2008, 02:30 PM
  4. Programmatically Select Item in ListBox or DropDownList
    By jmurrayhead in forum .NET Code Samples
    Replies: 0
    Last Post: June 27th, 2008, 11:05 AM
  5. Loop / Summary help
    By Rebelle in forum ASP Development
    Replies: 7
    Last Post: April 11th, 2008, 11:12 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO