DynamicValidator Control (General Reference)

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.

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." );


Community Additions

© 2016 Microsoft