ASP.net validation within nested user controls

Published on Friday, 31 January 2014

I have many User Controls throughout my sites, one of which being a date picker that I reuse.

I needed to validate against a the Start Date not being greater then the End Date, but being within two different User Controls I thought this was going to be difficult.

It was actually really straight forward. Using the built in .net validation, you can use the Compare Validator like:

<asp:CompareValidator ID="cvInputDates" runat="server" ControlToValidate="ucCalTo:txtCalendar" ControlToCompare="ucCalFrom:txtCalendar" ErrorMessage="The to date must be greater than the from date." Operator="GreaterThanEqual" Type="Date" Text="*" />

As you can see from the above, you use the ControlToValidate and ControlToCompare as normal, but you supply the UserControl name and then the name of the control within the control to validate.

Much simpler than I thought it was going to be.