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, 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 PropertyProxyValidator control, 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 attribute shown in the designer-generated source references the Customer class defined elsewhere in the application. 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;
using Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet;

protected void dateOfBirthValidator_ValueConvert(object sender, 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
Imports Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet

Protected Sub dateOfBirthValidator_ValueConvert(ByVal sender As Object, ByVal e As 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 following code shows the dateOfBirthTextBox control and the dateOfBirthValidator in an ASP.NET page. The OnValueConvert attribute of the PropertyProxyValidator specifies the name of the event handler shown in the previous code, and is located in the associated ASP.NET code-behind file.

<asp:TextBox ID="dateOfBirthTextBox"  />

<cc1:PropertyProxyValidator ID="dateOfBirthValidator"  
             ControlToValidate="dateOfBirthTextBox"
             PropertyName="DateOfBirth"
             RulesetName="RuleSetA"    
             SourceTypeName="ValidationAspNetQuickStart.Customer"
             OnValueConvert="dateOfBirthValidator_ValueConvert">
</cc1:PropertyProxyValidator></td>

The OnValueConvert property of the PropertyProxyValidator links the control to the type conversion handler named dateOfBirthValidator_ValueConvert.