How to create ASP.NET user/server control that uses a list of asp:ListItem as child controls?

后端 未结 2 1814
[愿得一人]
[愿得一人] 2020-12-31 17:00

I am looking to create a user/server control that will be created with something like the following:


   

        
2条回答
  •  温柔的废话
    2020-12-31 17:40

    You can add a property on a user control's code behind like:

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public List Items
    {
        get;
        set;
    }
    

    Your markup would then be:

    
      
        
      
    
    

    To make it so ListItem can be your own list item (Which I recommend doing as opposed to using asp.net.) You'll want to create your own class.

    Here's an example of a Server Control I use (I removed a lot of the noise as such this is just a skeleton):

       [ToolboxData("<{0}:Menubar runat=server>")]
        [System.ComponentModel.DesignTimeVisible(false)]
        public class Menubar : WebControl, IPostBackEventHandler
        {
    
            private List _menuItems = new List();
            [PersistenceMode(PersistenceMode.InnerProperty)]
            public List MenuItems
            {
                get
                {
                    return _menuItems;
                }
            }
    
        }
        [ToolboxItem(false)]
        [ParseChildren(true, "MenuItems")]
        public class MenuItem
        {
            private string _clientClick;
            private List _menuItems = new List();
    
            [Localizable(true)]
            public string Title { get; set; }
            public string Href { get; set; }
            public string Id { get; set; }
            [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
            public List MenuItems
            {
                get { return _menuItems; }
                set { _menuItems = value; }
            }
        }
    

    Now I can use this like:

    
        
            
            
                
                    
                    
                
            
        
    
    

提交回复
热议问题