Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Custom ListBox (WebControl derivated)

Custom ListBox (WebControl derivated)

Scheduled Pinned Locked Moved ASP.NET
designsysadminhelp
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dirso
    wrote on last edited by
    #1

    Hi, My Listbox control works fine, at design and run-time. One minor bug (if I can call it that way) is that the ListItems aren't displayed inside the control as the original Listbox does at design time. The collection works fine and they are shown at run-time fine too. I'm posting some code I think it's important, if you find usefull, I'll post the whole Listbox code. Just one more thing: I don't want the "Selected" property be available at design time. I tried [EditorBrowsable(EditorBrowsableState.Never)] but it didn't do anything. ListBox:

    public class ListBox : System.Web.UI.WebControls.WebControl
    {
        //...
        private ListItemDataCollection listData = null;
        public ListItemDataCollection ListItems
        {
            get
            {
                if (listData == null)
                {
                    listData = new ListItemDataCollection();
                }
                return listData;
            }
        }
    
        protected override void Render(HtmlTextWriter output)
        {
            // properties
            if (Rows > 1)
            {
                output.AddAttribute(HtmlTextWriterAttribute.Size, Rows.ToString());
                if (MultipleSelections)
                    output.AddAttribute(HtmlTextWriterAttribute.Multiple, true.ToString());
            }
    
            // calls the base method
            base.Render(output);
        }
    
        protected override void CreateChildControls()
        {
            Controls.Clear();
            for (int i =0; i < ListItems.Count; i++)
            {
                //ListItemData item = ListItems\[i\];
                //Controls.Add(new ListItem(item.Text, item.Value, i == SelectedIndex));
                Controls.Add(ListItems\[i\]);
            }
        }
    
        public override ControlCollection Controls
        {
            get
            {
                EnsureChildControls();
                return base.Controls;
            }
        }
    }
    

    ListItem:

    \[ToolboxItem(false),
    ToolboxData("<{0}:ListItem runat=server>"),
    TypeConverter(typeof(ExpandableObjectConverter))\]
    public class ListItem : System.Web.UI.Control
    {
        public ListItem(string text, string value)
            : base()
        {
            Text = text;
            Value = value;
        }
        public ListItem() { }
    
        public ListItem(string text, string value, bool selected)
            : th
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups