ASP.NET Custom/User Control With Children

Posted by Bob Fincheimer on Stack Overflow See other posts from Stack Overflow or by Bob Fincheimer
Published on 2010-06-15T12:07:57Z Indexed on 2010/06/15 12:12 UTC
Read the original article Hit count: 150

Filed under:

I want a create a custom/user control that has children (NOT a template control).

For Example, I want my control to have the following markup:

<div runat="server" id="div">
    <label runat="server" id="label"></label>
    <div class="field">
        <!-- INSERT CHILDREN HERE -->
    </div>
</div>

and when I want to use it on a page I simply:

<ctr:MyUserControl runat="server" ID="myControl">
    <span>This is a child</span>
    <div>And another <b>child</b>
</ctr:MyUserControl>

The child controls inside my user control will be inserted into my user control somewhere. What is the best way to accomplish this.

The functionality is similar to a asp:PlaceHolder but I want to add a couple more options as well as additional markup and the such. Also the child controls still need to be able to be accessed by the page.

© Stack Overflow or respective owner

Related posts about ASP.NET