Click here to Skip to main content
11,802,439 members (72,506 online)
Click here to Skip to main content

Does Not Allow Child Controls Error

, 16 Aug 2007 CPOL 25.4K 174 16
Rate this:
Please Sign up or sign in to vote.
When you create a custom drop-down-list that inherits from System.Web.UI.WebControls.DropDownList and when you try to add controls (e.g., Me.Controls.Add(myControl)), you will get a "does not allow child controls error". Here is the fix.

Screenshot - does-not-allow-child-controls.gif


I was trying to create a custom control that inherits from DropDownList with required field validation. My original intention was to embed RequiredFieldValidator on my custom drop-down-list control so that when I add a drop-down-list on my ASPX page, I don't have to re-code the RequiredFieldValidator all the time.

When I tried to add the required field validator as a child-control of the drop-down-list, I got the "does not allow child controls" error message as shown. Seems that the out-of-the-box drop-down-list does not allow child controls to be added.

Using the Code

I always have this principal when I do programming: "The biggest error on your application is normally caused by the-easiest-to-fix section". I spent hours yesterday to get this working, and guess what....the fix is very-very simple! Here it is. The code is in VB.NET.

What you have to do is override the CreateControlCollection method of the base class and create a new instance of it and pass the current class (Me for VB.NET, or this for C#).

''' <summary>
''' Override the CreateControlCollection to overcome the "does not allow child controls".
''' </summary>
''' <returns></returns>
''' <remarks></remarks>

Protected Overrides Function CreateControlCollection() As System.Web.UI.ControlCollection
    Return New ControlCollection(Me)
End Function

That's it! You can then do the following:


Points of Interest

Quiet easy, huh!? If you have the same problem when inheriting from out-of-the-box controls, this will be the fix.

Hope this helps.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Australia Australia
My name is Tommy Segoro currently working with Vivid Group in Perth, Western Australia. I also run my own hosting, web site design and development freelancing company. If you want to contact me please visit my website

I've been involved a lot with the customisation of Microsoft technologies such as Windows Sharepoint Services 2.0-3.0, Sharepoint Portal 2003, Microsoft Office Sharepoint Server 2007 (MOSS), Microsoft CRM and Microsoft Content Management Server (MCMS).

I currently hold Microsoft Certified Professional (MCP) and MS CRM Software Advisor certification and have been programming for almost 4 years now.

You may also be interested in...

Comments and Discussions

QuestionTrying to get this to work.. Pin
Himanshu Jain2-May-15 3:56
memberHimanshu Jain2-May-15 3:56 
GeneralThanks ! Pin
lagartron123mailcl25-Jun-14 7:46
memberlagartron123mailcl25-Jun-14 7:46 
QuestionThank you! Pin
Josh Mondragon27-Jan-12 5:23
memberJosh Mondragon27-Jan-12 5:23 
GeneralMy vote of 5 Pin
Member 740954110-Nov-10 0:57
memberMember 740954110-Nov-10 0:57 
GeneralMost likely that's not the error Pin
Ricardo Casquete26-Jul-09 18:25
memberRicardo Casquete26-Jul-09 18:25 
Generalthanks Pin
john mimiaga3-Oct-08 20:33
memberjohn mimiaga3-Oct-08 20:33 
GeneralThanks Pin
khalid Musaied5-Apr-08 20:53
memberkhalid Musaied5-Apr-08 20:53 
GeneralGreat tip - thank you so much Pin
Mark Rödicker7-Dec-07 1:07
memberMark Rödicker7-Dec-07 1:07 
GeneralOkay but... Pin
newkie21-Aug-07 5:17
membernewkie21-Aug-07 5:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 16 Aug 2007
Article Copyright 2007 by Tommy_Segoro
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid