Thursday, January 7, 2010

Composite Controls and Compare Validator control

Hi All,

While working with Asp.net validators for Custom controls i have found one issue of Microsoft ASP.NET validations controls.

Problem : I need to validate one composite control with value of other composite control/ Web control. I decided to go with CompareValidator control provided by ASP.NET default. Problem comes on ControlTovalidate and ControlToCompare properties, i assigned this properties as usual, here i assigned email.ID(where email is my composite control object).

By doing this i got the error like following,

"Control 'Contact_Email' referenced by the ControlToCompare property of 'cvContact_Email' cannot be validated."

Solution : By spending many hours finally i come out with solution to this. to achieve goal we need to require expose one public property of that Control's Child control( Textbox in my case). Now and on page you can assign UniqueId property to ControlToCompare property.

Here is code snippet for same,
cvConfirm is a CompareValidator object which i am creating runtime,

  276 cvConfirm.ControlToValidate = txtconfirm.UniqueID;

  277                                 cvConfirm.ControlToCompare = email.Refemailbox.UniqueID;



Here is a property which i have exposed within composite control,

   73 /// <summary>

   74         /// Gets the reference of this control.

   75         /// </summary>

   76         /// <value>The refemailbox.</value>

   77         [Bindable(true)]

   78         [Category("Appearance")]

   79         [DefaultValue("")]

   80         [Localizable(true)]       

   81         public TextBox Refemailbox

   82         {

   83             get

   84             {

   85                 EnsureChildControls();

   86                 return this._emailTextBox;

   87             }           

   88         }



I hope this would help those who faced same situation.

Happy Coding :)

No comments:

Post a Comment