Export (0) Print
Expand All

DynamicValidator Class

Updated: April 2009

Enforces and catches exceptions that are thrown in a data model and displays the error.

Namespace:  System.Web.DynamicData
Assembly:  System.Web.DynamicData (in System.Web.DynamicData.dll)

[ToolboxBitmapAttribute(typeof(DynamicValidator), "DynamicValidator.ico")]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class DynamicValidator : BaseValidator
<asp:DynamicValidator />

The DynamicValidator control can be used with data fields or data entities. It catches exceptions that are thrown in LINQ-to-SQL classes or entity in extensibility methods in the data model. The DynamicValidator control is associated with the controls that accept user input. For example, a DynamicValidator control that is associated with a text box that accepts user input for a UnitsInStock field in a Products table catches and displays the exception that is thrown if the input is greater or less than the allowable units in the data model.

By default, ASP.NET Dynamic Data does not display all exceptions from the data model in the page, because some database exceptions might contain confidential information. Dynamic Data displays ValidationException values only. If you want your application to display other exceptions, you can create a DynamicValidator control, provide the exceptions that you want to display, and attach the exceptions to the DynamicValidator control. The exceptions that are thrown in the data model will be displayed in all pages in the application.

The following example shows how to create a DynamicValidator control class that displays other exceptions in all pages.

[Visual Basic]

''' <summary> 
''' Display other exceptions in all pages. 
''' </summary> 
Public Class MyDynamicValidator 
    Inherits DynamicValidator 
    
    Protected Overloads Overrides Sub ValidateException(ByVal exception As Exception) 
        ' If it's not an exception that DynamicValidator displays find 
        ' the innermost exception. 
        If Not (TypeOf exception Is IDynamicValidatorException) AndAlso _
                Not (TypeOf exception Is ValidationException) Then 
            While exception.InnerException IsNot Nothing 
                exception = exception.InnerException 
            End While 
            
            ' Wrap it in a ValidationException so that the base code 
            ' does not ignore it. 
            If ExceptionShouldBeDisplayedInPage(exception) Then 
                exception = New ValidationException(Nothing, exception) 
            End If 
        End If 
        
        ' Call the base class for the exception that is modified.
        MyBase.ValidateException(exception) 
    End Sub 
    
    Private Function ExceptionShouldBeDisplayedInPage(ByVal e As Exception) _
            As Boolean 
        ' Add code to find the exception and 
        ' decide whether it should be shown in the page.
        
        Return True 
    End Function 
End Class

[C#]

/// <summary>
/// Display other exceptions in all pages. 
/// </summary>
public class MyDynamicValidator : DynamicValidator {

    protected override void ValidateException(Exception exception) {
        // If it's not an exception that DynamicValidator displays find 
        // the innermost exception.
        if (!(exception is IDynamicValidatorException) && !(exception 
              is ValidationException)) {
            while (exception.InnerException != null) {
                exception = exception.InnerException;
            }

            // Wrap it in a ValidationException so that the base code 
            // does not ignore it.
            if (ExceptionShouldBeDisplayedInPage(exception)) {
                exception = new ValidationException(null, exception);
            }
        }

        // Call the base class on the exception that is modified.
        base.ValidateException(exception);
    }

    private bool ExceptionShouldBeDisplayedInPage(Exception e) {
        // Add your code to find the exception and
        // decides whether it should be shown in the page.
        
           return true;
    }
}

The following example shows how to use tag remapping in the web.config file to display the exception in all the Web pages.

<pages>
  <tagMapping>
    <add tagType="System.Web.DynamicData.DynamicValidator" 
        mappedTagType="MyDynamicValidator"/>
  </tagMapping>
</pages>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1

Date

History

Reason

April 2009

Added more remarks and code example to the type.

Customer feedback.

July 2008

Added topic for new class.

SP1 feature change.

Community Additions

ADD
Show:
© 2014 Microsoft