How to add child nodes to custom asp.net user control derived from System.Web.UI.Control

前端 未结 2 1819
被撕碎了的回忆
被撕碎了的回忆 2021-01-05 13:31

I would like to know how to add some additional child nodes to a custom user control class derived from System.Web.UI.Control.

For example currently I have a cont

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-05 14:16

    You want to be able to describe asp.net control properties declaratively.

    To be able to have the following markup:

    
        
            
            
        
    
    

    You need the following code:

    [ParseChildren(true)]
    [PersistChildren(true)]
    [ToolboxData("<{0}:CustomControlUno runat=server>")]
    public class CustomControlUno : WebControl, INamingContainer
    {
        private Control1ChildrenCollection _children;
    
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Control1ChildrenCollection Children
        {
            get
            {
                if (_children == null)
                    _children = new Control1ChildrenCollection();
                return _children;
            }
        }
    }
    
    public class Control1ChildrenCollection : List
    {
    }
    
    public class Control1Child
    {
    
        public int IntegerProperty { get; set; }
        private string StringProperty { get; set; }
    }
    

提交回复
热议问题