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.

<asp:DynamicValidator
    AccessKey="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    ControlToValidate="string"
    CssClass="string"
    Display="None|Static|Dynamic"
    EnableClientScript="True|False"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    ErrorMessage="string"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
        Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    SetFocusOnError="True|False"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    Text="string"
    ToolTip="string"
    ValidationGroup="string"
    Visible="True|False"
    Width="size"
/>

Remarks

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.

Example

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
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

public partial class Product {

    partial void OnNameChanging(string value) {
        if (!Char.IsUpper(value[0])) {
           throw new Exception("Name must start with an uppercase letter."); 
        }

        int nameMinLength = 5;
        if (value.Trim().Length < nameMinLength) {
            throw new Exception("Name must be at least " +
                nameMinLength.ToString() + " characters long." );
        }
    }

}        

See Also

Reference

DynamicValidator