Thursday, March 22, 2012

ListBox (WebControls)

How can I remove (using C#) the items previously added to a System.Web.UI.WebControls.ListBoxTo remove all the items, use the following:

foreach(ListItem item in ListBox1.Items)
{
ListBox1.Items.Remove(item);
}

If your just wanting to remove a single item you can use the following:

ListBox1.Items.RemoveAt(n); where n is the index of the item you want to
remove.

If you don't know the index of the item, then you can use:

ListItem item = ListBox1.Items.FindByValue(foo); where foo is a string
representing the value you're looking for.
ListBox1.Items.Remove(item);
or;
ListItem item = ListBox1.Items.FindByText(foo); where foo is a string
representing the text you're looking for.
ListBox1.Items.Remove(item);

Of course, replace "ListBox1" with the name of your ListBox control.

David Young

"mg" <mg@.theworld.com> wrote in message
news:D7718CBA-414C-4F62-9701-3F49EBE1D9BF@.microsoft.com...
> How can I remove (using C#) the items previously added to a
System.Web.UI.WebControls.ListBox

0 comments:

Post a Comment