Enterprise Library 3.0 Validation Application Block - ASP.NET PropertyProxyControl ValueConvertEvent
by David Hayden ( Florida MVP C# ), Filed: Enterprise Library 3.0
Here is a quick tip to fix conversion errors when using the ASP.NET PropertyProxyValidator Web Control that comes with the Validation Application Block in Enterprise Library 3.0.
There may be times when you receive the following error during validation in ASP.NET:

In this case, the Int32RangeValidator that checks for an age between 1-99 could not convert the string.Empty to an integer and do the validation.
For cases such as this, the PropertyProxyValidator Web Control that comes with the Validation Application Block has an event called, ValueConvert, that allows you to massage the input a bit prior to validation:

In the code below, I do a bit of conversion in the event to avoid the error. Note I am doing a little extra here just to show the possibilities.
First, if the input is string.Empty, I convert it to a zero and ask the Validation Application Block to validate zero. I set the value I want it to validate as e.ConvertedValue.
If it is not string.Empty, I do a Int32.TryParse on the value. If it parses fine, I set the value to validate as e.ConvertedValue. If it does not parse, I skip the validation and tell the PropertyProxyValidator to display an error message, “1-99 Please...“.
protected void ageValidator_ValueConvert(object sender,
ValueConvertEventArgs e)
{
if (string.IsNullOrEmpty(e.ValueToConvert))
{
e.ConvertedValue = 0;
return;
}
int convertedValue;
if (Int32.TryParse(e.ValueToConvert, out convertedValue))
e.ConvertedValue = convertedValue;
else
e.ConversionErrorMessage = "1-99 Please...";
}
Conclusion
The PropertyProxyValidator ValueConvert Event offers one the opportunity to intercept the value to validate and do a little massaging to avoid conversion errors.
Validation Application Block Tutorials
Source: David Hayden ( Florida MVP C# )
Filed: Enterprise Library 3.0