Integrating with ASP.NET

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

You can integrate the Validation Application Block with ASP.NET applications. For example, you can use the application block to validate information a user enters into a Web form. ASP.NET is integrated with the Validation Application Block through the types defined in the Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet assembly.

The PropertyProxyValidator class defined in this assembly provides the main integration point. Its purpose is to check an ASP.NET control's value using the validators that are included in a user-provided application class. The PropertyProxyValidator class acts as a wrapper that links a control to a validator in an application-level class.

The PropertyProxyValidator class provides an OnValueConvert property that allows you to specify a handler for converting values entered by the user to values required by the validators. If you do not specify a handler then a default conversion is performed by the ASP.NET TypeConverter services. For an example of how to use the PropertyProxyValidator class to validate user input in an ASP.NET application, see Validation QuickStart.

To use the PropertyProxyValidatorcontrol, you must manually add the code to an .aspx file, as shown in the following example.

<cc1:propertyproxyvalidator
id="firstNameValidator"

ControlToValidate="firstNameTextBox"
PropertyName="FirstName"
RulesetName="RuleSetA" SourceTypeName="ValidationQuickStart.BusinessEntities.Customer">
</cc1:propertyproxyvalidator>

The SourceTypeName XML attribute shown in the designer-generated XML code references the Customer application class. This is the class whose validators will be used.

When using ASP.NET with the Validation Application Block, you may need to convert data types such as dates before you can validate the data. The integration library provides a way for you to insert code that performs this conversion. To do this, you need to provide an event handler for the conversion and then reference this handler in the .aspx file.

The following code excerpt shows a handler that converts a date of birth string into a date.

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
protected void dateOfBirthValidator_ValueConvert(object sender,  
Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet.ValueConvertEventArgs e)
    {
        string value = e.ValueToConvert as string;
        try
        {
            e.ConvertedValue = DateTime.Parse(value, 
                                 System.Globalization.CultureInfo.CurrentCulture);
        }
        catch
        {
            e.ConversionErrorMessage = 
                          "Date Of Birth is not in the correct format.";
            e.ConvertedValue = null;
        }
    }
'Usage
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.Validation
Imports Microsoft.Practices.EnterpriseLibrary.Validation.Validators
Protected Sub dateOfBirthValidator_ValueConvert(ByVal sender As Object, ByVal e As Microsoft.Practices.EnterpriseLibrary.Validation.Integration.ValueConvertEventArgs) Handles dateOfBirthValidator.ValueConvert
    Dim stringValue As String = CStr(e.ValueToConvert)
    Dim dateValue As DateTime

    Dim success As Boolean = DateTime.TryParse(stringValue, dateValue)
    If success Then

        e.ConvertedValue = dateValue
    Else

        e.ConversionErrorMessage = "Date Of Birth is not in the correct format."
        e.ConvertedValue = Nothing
    End If
End Sub

The next code excerpt shows the dateOfBirthValidator and the dateOfBirthTextBox control together on an .aspx page. The OnValueConvert attribute of the PropertyProxyValidator specifies the name of the event handler to use (the code for which is located on the associated code-behind page).

...<tr>
   <td style="width: 100px">
       Date Of Birth:</td>
   <td style="width: 508px">
       <asp:TextBox ID="dateOfBirthTextBox" ></asp:TextBox><br />
       <cc1:PropertyProxyValidator ID="dateOfBirthValidator"  
                ControlToValidate="dateOfBirthTextBox"
                OnValueConvert="dateOfBirthValidator_ValueConvert" 
                PropertyName="DateOfBirth"
                RulesetName="RuleSetA"    
                SourceTypeName="ValidationAspNetQuickStart.Customer">
        </cc1:PropertyProxyValidator></td>
</tr>
...

The OnValueConvert property of the PropertyProxyValidator links the control to the conversion handler defined in the previous example.

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.