DataTypeValidator 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 data-type validation 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:DataTypeValidator runat="server" 
    ControlToValidate="uxFirstNameTextBox" 
    Type="Date"
    Entity="Start Date" 
    ValidationGroup="MainValidationGroup"
    />

Source Code

VB.NET

Imports Microsoft.VisualBasic
Imports System.Web.UI.WebControls

Namespace AcmeBroomCompany

    Public Class DataTypeValidator

        Inherits System.Web.UI.WebControls.CompareValidator

        Private _entity As String
        Public Sub New()

            Display = ValidatorDisplay.Dynamic
            EnableClientScript = True
            SetFocusOnError = True
            Text = ""
            [Operator] = ValidationCompareOperator.DataTypeCheck

            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

                If Me.Type = ValidationDataType.String Then

                    Me.ErrorMessage = "<span style='font-size:12pt;font-weight:bold;'>" & _
                                "DEVELOPER: Set the Entity property AFTER the Type property " & _
                                "for " & _entity & "</span>"

                Else

                    Me.ErrorMessage = _entity & " must be a valid " & Me.Type.ToString()

                End If

            End Set
        End Property

    End Class

End Namespace

C#

TODO