CustomValidator Class

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

Overview

Subclasses the standard ASP.NET CustomValidator control.

Sample Implementation

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

ASPX Markup

The following code demonstrates how to place the control on an ASP.NET page.

<abc:CustomValidator ID="uxLinesValidator" runat="server" 
    ClientValidationFunction="validateLines"
    OnServerValidate="uxLinesValidator_Validate"
    ErrorMessage="Exactly one primary line must be specified."
    />

JavaScript

The following code demonstrates client-side validation.

function validateLines(sender, e)
{
    e.IsValid = ($("span#uxLinesGridViewSpan>div>table").length > 0);
}

Code-Behind

The following code demonstrates server-side validation.

Protected Sub uxLinesValidator_Validate(ByVal source As Object, ByVal args As ServerValidateEventArgs)

    Dim primaryRows As Integer = 0

    For Each gvRow As GridViewRow In uxLinesGridView.Rows

        If gvRow.RowType = DataControlRowType.DataRow Then

            Dim uxPrimaryCheckBox As CheckBox = _
                        CType(gvRow.FindControl("uxPrimaryCheckBox"), CheckBox)

            If uxPrimaryCheckBox IsNot Nothing AndAlso uxPrimaryCheckBox.Checked Then
                primaryRows += 1
            End If

        End If

    Next

    args.IsValid = (primaryRows = 1)

End Sub

Source Code

VB.NET

Imports Microsoft.VisualBasic

Namespace AcmeBroomCompany

    Public Class CustomValidator

        Inherits System.Web.UI.WebControls.CustomValidator

        Private _entity As String

        Public Sub New()

            Display = ValidatorDisplay.Dynamic
            Text = ""
            SetFocusOnError = True
            EnableClientScript = False

            Dim img As Image = New Image()
            img.ImageUrl = "~/images/Exclamation.png"
            Me.Controls.Add(img)

            _entity = ""

        End Sub

        Public Property Entity() As String
            Get
                Return _entity
            End Get
            Set(ByVal value As String)
                _entity = value
            End Set
        End Property

        Public Overloads Property ClientValidationFunction() As String
            Get
                Return MyBase.ClientValidationFunction
            End Get
            Set(ByVal value As String)
                If value.Length > 0 Then
                    MyBase.ClientValidationFunction = value
                    Me.EnableClientScript = True
                Else
                    MyBase.ClientValidationFunction = ""
                    Me.EnableClientScript = False
                End If
            End Set
        End Property

        Public Overloads Property ErrorMessage() As String
            Get
                Return MyBase.ErrorMessage
            End Get
            Set(ByVal value as String)
                MyBase.ErrorMessage = value
                MyBase.Text = "<img src='images/Exclamation.PNG' />"
            End Set
        End Property

    End Class

End Namespace

C#

TODO