This documentation is archived and is not being maintained.

DynamicValidator Control (General Reference)

The DynamicValidator control is part of the ASP.NET Dynamic Data framework. The control catches exceptions that are thrown from the data model during validation and raises the exception as a validation event on the Web page.

    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    ForeColor="color name|#dddddd"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"

The DynamicValidator control is used in many of the Edit field templates in the DynamicData\FieldTemplates directory. You can apply business rules to the extensibility methods of data-model objects and perform validation when an update or insert operation occurs.

The following example shows how to implement two rules for the Name field of the Products table in the AdventureWorksLT database. The Name field must start with an uppercase letter, and the name must be at least five characters long. Update or insert operations cause validation of the Name field. If an exception is thrown, the exception is displayed on the update or insert Web page.

Imports Microsoft.VisualBasic
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations

Partial Public Class Product

    Private Sub OnNameChanging(ByVal value As String)
        If (Not Char.IsUpper(value(0))) Then
            Throw New Exception("Name must start with an uppercase letter.")
        End If

        Dim nameMinLength As Integer = 5
        If (value.Trim().Length < nameMinLength) Then
            Throw New Exception("Name must be at least " & _
                nameMinLength.ToString() & " characters long.")
        End If
    End Sub

End Class