I have created my ASP.NET UserControl which has a asp:DropDownList inside.
<asp:DropDownList ID="ddlExamYear" runat="server"></asp:DropDownList>
In a webpage, I add this UserControl to the page and also a RequiredFieldValidator to validate the control at page-level.
Actually, I can put the validation inside the control itself, but to make it flexible and general enough to use elsewhere, I decided to move the validation to the page-level instead.
However, at first I use this markup to validate the control:
<uncc:ExamYearDropdown ID="ddlExamYear" Width="100" runat="server"></uncc:ExamYearDropdown> <asp:RequiredFieldValidator ID="ReqExamYear" runat="server" ControlToValidate="ddlExamYear" InitialValue="-1" Display="None" ErrorMessage="Please select Exam Year" ValidationGroup="ValForm"></asp:RequiredFieldValidator>
But no error message shown after the validation is called.
After debugging the javascript for the validation, ControlToValidate property generate the wrong element ID of the control, “dnn_ctr433_ManageLCELoader_ctl00_ddlExamYear“. The ID seems to be the wrapper of the usercontrol not the dropdown inside which has ID “dnn_ctr433_ManageLCELoader_ctl00_ddlExamYear_ddlExamYear“.
To make the validator access the child control inside the usercontrol is
“To add $ after the usercontrol ID and follow by the child control ID”
<uncc:ExamYearDropdown ID="ddlExamYear" Width="100" runat="server"></uncc:ExamYearDropdown>
<asp:RequiredFieldValidator ID="ReqExamYear" runat="server" ControlToValidate="ddlExamYear$ddlExamYear"
InitialValue="-1"
Display="None" ErrorMessage="Please select Exam Year"
ValidationGroup="ValForm"></asp:RequiredFieldValidator>
A strand of hair before the eye obscures whole mountains
Reference: Answer from Sjoerd in http://stackoverflow.com/questions/359422/web-user-controls-and-validation