Custom ListBox (WebControl derivated)
ASP.NET
1
Posts
1
Posters
0
Views
1
Watching
-
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