4

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.

5
  • i think you cant do this... But yo can do it in .ascx of ur user control Commented Dec 16, 2009 at 4:26
  • Can you specify a bit please? I have <%=title%> and <%=content%> in my ascx file which works fine, but it's a pain to pass custom HTML to the content property. Commented Dec 16, 2009 at 4:29
  • System.Web.UI.UserControl doesnot have a property textbox Commented Dec 16, 2009 at 4:30
  • You can dynamically add controls to your user control Commented Dec 16, 2009 at 4:32
  • 1
    Why aren't you putting these tags and such inside the user control's own (ascx) file? Or is this a custom control instead of an actual user control? Commented Dec 16, 2009 at 7:12

2 Answers 2

7

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>
Sign up to request clarification or add additional context in comments.

Comments

0

You need to make sure EnsureChildControls is called. There are number of ways of doing that such as through the CreateChildControls base method but you can simply do this to get the inner contents of a custom control to render. It gets more complicated when you need to remember state and trigger events but for plain HTML this should work.

protected override void Render(HtmlTextWriter writer) {
    EnsureChildControls();
    base.Render(writer);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.