ValidationSummary Class - ASP.NET

This page is part of the Class Library Pages collection.
Click the icon to see the index.
See also Validation Controls

Features

Provides standardized validation summarization across a website.

Sample Implementation

To use this control, you need to follow the instructions in the Consuming Custom Controls in ASP.NET article.

ASPX Markup

<abc:ValidationSummary runat="server" ValidationGroup="MainValidationGroup" />

Source Code

VB.NET

Namespace AmericanBroomCompany
    Public Class ValidationSummary
        Inherits System.Web.UI.WebControls.ValidationSummary
        Public Sub New()
            EnableClientScript = True
            HeaderText = "<b><center>Please correct the following item(s).</center></b>"
            ShowMessageBox = False
            ShowSummary = True
            DisplayMode = ValidationSummaryDisplayMode.BulletList
            BackColor = System.Drawing.Color.FromArgb(255, 255, 153)
            BorderColor = System.Drawing.Color.Red
            BorderWidth = New Unit(2D, UnitType.Pixel)
            Font.Bold = False
            Font.Size = New FontUnit(9)
        End Sub
        Public ReadOnly Property ErrorSummary() As String

            Get

                Dim result As String = ""

                For Each v As BaseValidator In Me.Page.Validators

                    If v.ValidationGroup = Me.ValidationGroup And Not v.IsValid Then

                        Select Case Me.DisplayMode
                            Case ValidationSummaryDisplayMode.BulletList
                                result &= "<li>" & v.ErrorMessage & "</li>"
                            Case ValidationSummaryDisplayMode.List
                                result &= v.ErrorMessage & "<br/>"
                            Case ValidationSummaryDisplayMode.SingleParagraph
                                result &= v.ErrorMessage & " "
                        End Select

                    End If

                Next

                Return result

            End Get

        End Property
    End Class
End Namespace

C#

namespace AmericanBroomCompany
{
    public class ValidationSummary : System.Web.UI.WebControls.ValidationSummary
    {
        public ValidationSummary()
        {
            this.EnableClientScript = true;
            this.HeaderText = "<b><center>Please correct the following item(s).</center></b>";
            this.ShowMessageBox = false;
            this.ShowSummary = true;
            this.DisplayMode = ValidationSummaryDisplayMode.BulletList;
            this.BackColor = System.Drawing.Color.FromArgb(255,255,153);
            this.BorderColor = System.Drawing.Color.Red;
            this.BorderWidth = new Unit(2D, UnitType.Pixel);
            this.Font.Bold = false;
            this.Font.Size = new FontUnit(9);
        }
        public string ErrorSummary
        {
            get
            {
                string result = "";
                foreach (BaseValidator v in this.Page.Validators)
                {
                    if (v.ValidationGroup == this.ValidationGroup & !v.IsValid)
                    {
                        switch (this.DisplayMode)
                        {
                            case ValidationSummaryDisplayMode.BulletList:
                                result += "<li>";
                                result += v.ErrorMessage;
                                result += "</li>";
                                break;
                            case ValidationSummaryDisplayMode.List
                                result += v.ErrorMessage;
                                result += "<br/>";
                                break;
                            case ValidationSummaryDisplayMode.SingleParagraph
                                result += v.ErrorMessage;
                                result += " ";
                                break;
                        }
                    }
                }
                return result;
            }
        }
    }
}