ListBox (WebControls)

G

Guest

How can I remove (using C#) the items previously added to a System.Web.UI.WebControls.ListBox
 
D

dm_dal

To 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 said:
How can I remove (using C#) the items previously added to a
System.Web.UI.WebControls.ListBox
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top