views:

160

answers:

2

Is it possible to build a UserControl that takes both ListItems and a ContentTemplate? Something like this:

<Custom:UserControl ID="ucTest" runat="server">
  <Items>
    <asp:ListItem Text="Test1" Value="1" />
    <asp:ListItem Text="Test2" Value="2" />
  </Items>
  <ContentTemplate>
    Here is some content!<br/>
    <asp:Button ID="btnTest" runat="server" OnClick="SomeFunc" />
   </ContentTemplate>
</Custom:UserControl>
+1  A: 

Hey,

This example shows you how to setup a custom control to have multiple children like you desire: http://www.codeproject.com/KB/webforms/MikEllASPNetQuery.aspx. I posted a custom control example, because I'm pretty sure that you cannot with a user control.

THe contenttemplate can be an ITemplate property with a getter/setter, while the other you could maybe take advantage of the ListItemCollection class. Define each property as defined in the article (it's an example with multiple inner object references).

HTH.

Brian
Great link - thank you!
Chris
+1  A: 

I combined them like so and it appears to be working correctly:

   [ParseChildren(true), PersistChildren(false)]
    public class Test : WebControl, INamingContainer
    {
        [ParseChildren(true, "Items")]
        public class iTestItems
        {
            private ListItemCollection _Items;

            [DefaultValue((string)null), MergableProperty(false), PersistenceMode(PersistenceMode.InnerDefaultProperty)]
            public virtual ListItemCollection Items
            {
                get
                {
                    if (_Items == null)
                        _Items = new ListItemCollection();

                    return _Items;
                }
            }
        }

        private iTestItems _TestItems = null;
        private ITemplate _ContentTemplate = null;
        public event EventHandler TestClick = null;

        [PersistenceMode(PersistenceMode.InnerProperty),
         TemplateContainer(typeof(iTestItems)),
         TemplateInstance(TemplateInstance.Single)]
        public iTestItems TestItems
        {
            get { return _TestItems; }
            set { _TestItems = value; }
        }

        [PersistenceMode(PersistenceMode.InnerProperty),
         TemplateContainer(typeof(TemplateControl)),
         TemplateInstance(TemplateInstance.Single)]
        public ITemplate ContentTemplate
        {
            get { return _ContentTemplate; }
            set { _ContentTemplate = value; }
        }

    }

Used:

<cc:Test ID="jqTestTest01" runat="server" OnTestClick="jqTestTest01_TestClick">
    <TestItems>
        <asp:ListItem Text="Tab One" Value="1" Selected="True" />
        <asp:ListItem Text="Tab Two" Value="2" />
        <asp:ListItem Text="Tab Three" Value="3" />
        <asp:ListItem Text="Tab Four" Value="4" />
        <asp:ListItem Text="Tab Five" Value="5" />
    </TestItems>
    <ContentTemplate>
        <asp:Label ID="lblTestTest01" runat="server" Text="None" />            
    </ContentTemplate>    
</cc:Test>
Chris