I am making a composite control for users to enter dates. It consists of drop-down lists for the month and day, and a textbox for the year. I have a method I wrote that checks if the date is valid (i.e., entering Feb 29 is only allowed on leap years), but validating the year textbox itself can be done with just a RangeValidator. Here is some C# code from my CreateChildControls method:
this
.Controls.Add(drpLstMonth);
this.Controls.Add(drpLstDay);
this.Controls.Add(txtYear);
//validator to make sure the user can only enter a 4-digit number for the year
vldtrYear.ControlToValidate = "txtYear";
//other validator properties are set here, removed b/c they are irrelevant to this post
this.Controls.Add(vldtrYear);
When I run a page that uses an instance of this control, I get an error within the browser saying: Unable to find control id 'txtYear' referenced by the 'ControlToValidate' property of ''.
I thought maybe the control's ID was being renamed at runtime since it's within a composite control, so I changed the line
vldtrYear.ControlToValidate = "txtYear";
to:
vldtrYear.ControlToValidate = Controls[2].ClientID;
It seems I was right about the control's ID being renamed, but that didn't fix the problem. I got the same error message, except 'txtYear' had become 'installDate_ctl02'.
I also find it odd that the error message calls the validator ". instead of vldtrYear or even installDate_ctl03.
For now I have fixed the problem easily enough with an error message label and a validation method I wrote myself, but I'd like to know how to get regular validators working in composite controls for future reference.