views:

393

answers:

4

I'm looking for ideas on how to use a custom validation control for an asp:ChangePassword control. A simple attempt to set the "ControlToValidate" property on the asp:CustomValidator control results in:

Control 'changePassword1' referenced by the ControlToValidate property of 'passwordValidation' cannot be validated.

Thanks.

+1  A: 

You can validate anything with CustomValidator if you use javascript to get the controls directly. Not being able to set ControlToValidate means that you don't get the data in the EventArgs and have to get it yourself.

jrcs3
A: 

You need to change the ChangePassword control to be a templated control in order to use a custom validator. Be sure to set the custom validator's validation group to be the same as the other controls in the template. Alternatively, the ChangePassword control has a NewPasswordRegularExpression property that may meet your needs.

Jamie Ide
A: 

Yes, just use templates:

http://quickstarts.asp.net/QuickStartv20/util/srcview.aspx?path=~/aspnet/samples/ctrlref/login/ChangePasswordTemplates.src

http://quickstarts.asp.net/quickstartv20/aspnet/doc/ctrlref/login/changepassword.aspx

As long as the textbox-id is "Username" (or whatever you want to validate), you can add any kind of validator to the template.

Stefan
+1  A: 

The fundamental problem that leads to this error message is that the ChangePassword control doesn't have a "ValidationProperty" attribute set at the class level. As mentioned earlier, you can just leave out the ControlToValidate attribute from your CustomValidator. This is probably the best plan for the ChangePassword control.

If you are trying to use CustomValidator with a custom control, set the ValidationProperty on your custom control's class:

[DefaultProperty("StateName"), ValidationProperty("StateName")]
public class StateBox: CompositeControl
{
    ...

If you want to use CustomValidator with a library control that doesn't have the ValidationProperty attribute set, derive a class from the library control and set the ValidationProperty on your derived class.

P.J. Tezza