How do you build an ASP.NET custom control with a collection property?

五迷三道 提交于 2019-11-29 01:45:49
Chris Pietschmann

Here's a really simple example control that does exactly what you are looking for:

namespace TestControl
{
    [ParseChildren(true, DefaultProperty = "Names")]
    public class MyControl : Control
    {
        public MyControl()
        {
            this.Names = new List<PersonName>();
        }

        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public List<PersonName> Names { get; set; }
    }

    public class PersonName
    {
        public string Name { get; set; }
    }
}

And, here is an example usage:

<%@ Register Namespace="TestControl" TagPrefix="TestControl"  %>

<TestControl:MyControl runat="server" ID="MyControl1">
    <TestControl:PersonName Name="Chris"></TestControl:PersonName>
    <TestControl:PersonName Name="John"></TestControl:PersonName>
</TestControl:MyControl>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!