locked
Writing container controls RRS feed

  • Question

  • User967840515 posted
    How can you write a custom control that can contain other controls?

    As a trivial example, say I wanted a red square that could hold an asp:Button.

    The control would be something like
    [DefaultProperty("Text"), ParseChildren(true, "Text")]
    public class RedSquare : WebControl
    {
        ...
    }
    
    and the RenderContents method could just render a div with a red background and the "Text" property in it.

    This seems to work fine for normal text but I can't add child controls to it. For example, I can't do
    <mycontrol:RedSquare runat="server"><asp:Button Text="press me" runat="server" /></mycontrol:RedSquare>

    Is there any way to do this so that I can put a control <...>here</...>
    Saturday, August 30, 2008 10:44 AM

Answers

All replies

  • User2032526919 posted

    Hi,

    that would be a templated server control e.g a control having template property to which you can put other controls / speciofy them in markup and they are added and used by the control

    I've written some posts in the past demonstrating such:

    ASP.NET: Implementing designer for a templated server control
    http://aspadvice.com/blogs/joteke/archive/2006/08/13/20573.aspx

    Templated Composite Controls
    http://aspadvice.com/blogs/joteke/archive/2006/11/19/Templated-composite-controls.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 30, 2008 12:07 PM
  • User481221548 posted

    Hi there

    Using a Panel with a custom CSS-class would be the easiest one :-)
    For all other things i agree with teemu and would too using templates for that.

    Sunday, August 31, 2008 11:09 PM
  • User967840515 posted

    Hi there

    Using a Panel with a custom CSS-class would be the easiest one :-)

    Unfortunately, a red square was a trivial example. What I want to do is not possible with just a panel and some CSS.

    I'll look into templates.
    Monday, September 1, 2008 3:58 PM
  • User967840515 posted
    I've tried these but I can't seem to get any further than I already was. If I take the simple example from the top of http://aspadvice.com/blogs/joteke/archive/2006/08/13/20573.aspx and then use it as follows:
    <%@ Register Namespace="TemplateControls" TagPrefix="templates" %>
    
    ...
    
    <templates:SimpleTemplateControl ID="mytemplate" runat="server">
        <asp:Label ID="mylabel" runat="server">foo</asp:Label>
    </templates:SimpleTemplateControl>
    It gives me an error on the asp:Label line:
    Parser Error Message: Type 'TemplateControls.SimpleTemplateControl' does not have a public property named 'Label'.

    If I change that to
    <templates:SimpleTemplateControl ID="mytemplate" runat="server">foo</templates:SimpleTemplateControl>
    then I get the error:
    Parser Error Message: Literal content ('foo') is not allowed within a 'TemplateControls.SimpleTemplateControl'.
    Edit: Never mind, I didn't realise that you needed to use
    <templates:SimpleTemplateControl ID="mytemplate" runat="server">
        <SimpleTemplate>... foo ...</SimpleTemplate>
    </templates:SimpleTemplateControl>
    Thanks :)

    P.S. Has anyone else noticed that on this board the "Preview" tab seems to produce normal sized text but the topic itself seems to make it significantly smaller?
    Monday, September 8, 2008 2:08 PM