PropertyProxyValidator for <asp:DropDownList />

Topics: Validation Application Block
Dec 20, 2007 at 11:13 PM
Is there any hack for this: I want PropertyProxyValidator to validate selected value in <asp:DropDownList /> - it's just "<select>" html tag...
PropertyProxyValidator works only for TextBox.

Dec 24, 2007 at 5:03 PM
There is a property on the validators to pass in the name of a method that can be used to convert the value being passed into a validator. In the case of a DDL I use an Int_ValueConvert() method that basically takes the DDL.SelectedValue and converts it to an int from a string.

Dec 26, 2007 at 2:36 PM
I think I don't understand. :(
Can You give me a brief instructions what should I do...

Domagoj Barisic
Dec 26, 2007 at 4:05 PM
The proxy validator will take the selected value from a DDL and pass it to the validator. In my case I needed integers from the DDL selected values so I did something similar to the following

Add following to aspx page:

<asp:DropDownList ID="ddlMonthOfYear" runat="server"></asp:DropDownList><br />
<cc1:PropertyProxyValidator ID="ddlMonthOfYearValidator" runat="server"

In the code behind add:

protected void Int_ValueConvert(object sender, ValueConvertEventArgs e)
// Convert the DDL.SelectedValue to an integer
string value = e.ValueToConvert as string;

int convertedValue;
if(Int32.Parse(value, convertedValue))
// DDL Selected Value has now been converted to an Int
e.ConvertedValue = convertedValue;
e.ConversionErrorMessage = "Selected value is not in the correct format
e.ConvertedValue = null;
e.ConversionErrorMessage = "Selected value is not in the correct format
e.ConvertedValue = null;

The important things to note are the properties set on the validator in the aspx page, the name of the property and the type links to the validation ruleset. The conversion method converts from the selected value to an int. And the control to validate links to the DDL control.

The Entlib help file Validation Block -> Key Scenarios -> Integrating the VAB -> ASP.NET has more information about the above.

Dec 27, 2007 at 9:12 AM
Edited Dec 27, 2007 at 9:16 AM
Thanks for this example, It works very well! I played with "IntValueConvert" everything works just fine except I run into another issue: I am using partial postback with ASP.NET AJAX to send Form data and ServerSideValidationExtender which dosen't fire "IntValueConvert" ("Int_ValueConvert" fires only on full postback...) so I can't validate Dropdown list, only textboxes....
I assume that best way to solve this problem is to use full postback to send Form data...bye bye ajax :-(
Thx anyway...

Domagoj Barisic