English 中文(简体)
UserControl Combined ListItems & ContentTemplate
原标题:

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>
问题回答

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>

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签