2011/01/30

Client side validation of a control defined inside a web user control in ASP.NET

I came across the issue of validating user input using ASP.NET client-side validators, the requirement was to validate the whole page controls with client-side validation; including controls (textboxes, dropdowns, etc) that are defined inside web user controls.

Apparently, the ASP.NET client validators that are defined in the page but outside the user controls will not recognize the control ID inside the user control; so the solution was to point to the correct ID of that control in the client validator using the ControlToValidate property.

In this case the ID of the control will be: UserControlID$ControlID.

The definition of the client side validation will be as follows:

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ValidationGroup="ValidationGroup1" ControlToValidate="UserControlID1$TextBox1"></asp:RequiredFieldValidator>

This also works with the ValidationGroup property as well, provided that the ValidationGroup property for the control(s) inside the user control are set. This can be accomplished through a set property defined for the user control that will set its child controls accordingly.

No comments:

Post a Comment