Is there a way to put inner controls inside a ASP.NET Custom Control?

前端 未结 3 564
猫巷女王i
猫巷女王i 2020-12-31 14:19

I want to do something like (Updated example):


  
    
    

        
3条回答
  •  自闭症患者
    2020-12-31 14:58

    Use the ParseChildrenAttribute and PersistChildrenAttribute attributes:

    [ParseChildren(false)]
    [PersistChildren(true)]
    public class MyControl : UserControl { }
    

    This will cause any controls you put inside the reference:

    
      
    
    

    To be appended to the end of the Controls collection of your UserControl contents.

    However, if you want to have a collection of controls, you should probably use a server control and not a user control. For a control that works like this:

    
        
            
        
    
    

    You need a Control class that has a Tabs property; the Tabs property should be a Collection; and it should contain objects of type Tab. I've created the three classes here:

    [ParseChildren(true, "Tabs")]
    public class TabControl: WebControl, INamingContainer
    {
        private TabCollection _tabs;
    
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public TabCollection Tabs
        {
            get
            {
                if (_tabs == null)
                {
                    _tabs = new TabCollection();
                }
                return _tabs;
            }
        }
    
        protected override void Render(HtmlTextWriter writer)
        {
            foreach (Tab tab in Tabs)
            {
                writer.WriteBeginTag("div");
                writer.WriteAttribute("class", tab.CssClass);
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.Write("this is a tab called " + tab.Title);
                writer.WriteEndTag("div");
            }
        }
    }
    

    And the tab class:

    public class Tab
    {
        public string CssClass { get; set; }
        public string Title { get; set; }
    }
    

    And the tab collection:

    public class TabCollection : Collection { }
    

提交回复
热议问题