Jaa


Integrating with ASP.NET

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 raises the ValueConvert event when it needs to convert the type of a value entered into the associated edit control. You can handle this event to provide a custom type converter 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.

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"
     runat="server"
     ControlToValidate="firstNameTextBox"
     PropertyName="FirstName"
     RulesetName="RuleSetA"
     SourceTypeName="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;
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
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="Customer"
             OnValueConvert="dateOfBirthValidator_ValueConvert">
</cc1:PropertyProxyValidator>