views:

507

answers:

1

Hey,

I have a user control which accepts a title attribute. I would also like that input inner HTML (ASP Controls also) inside of that user control tag like so:

<uc:customPanel title="My panel">
     <h1>Here we can add whatever HTML or ASP controls we would like.</h1>
     <asp:TextBox></asp:TextBox>
</uc:customPanel>

How can I achieve this? I have the title attribute working correctly.

Thanks.

+2  A: 

Implement a class that extends Panel and implements INamingContainer:

public class Container: Panel, INamingContainer
{
}

Then, your CustomPanel needs to expose a property of type Container and another property of type ITemplate:

public Container ContainerContent
{
    get
    {
       EnsureChildControls();
       return content;
    }
}
[TemplateContainer(typeof(Container))]
[TemplateInstance(TemplateInstance.Single)]
public virtual ITemplate Content
{
    get { return templateContent; }
    set { templateContent = value; }
}

Then in method CreateChildControls(), add this:

if (templateContent != null)
{
    templateContent.InstantiateIn(content);
}

And you will be using it like this:

<uc:customPanel title="My panel"> 
    <Content>    
        <h1>Here we can add whatever HTML or ASP controls we would like.</h1>
        <asp:TextBox></asp:TextBox>
     </Content>
</uc:customPanel>
joerage