asp.net: How can I remove an item from a dropdownl

2019-02-11 15:11发布

I have a dropdownlist, and in some cases need to remove an item (in the code-behind). I need to remove the item based on the value of the item.

How can I do this?

9条回答
Animai°情兽
2楼-- · 2019-02-11 15:35
myDropDown.Items.Remove(myDropDown.Items.FindByText("TextToFind"))
查看更多
The star\"
3楼-- · 2019-02-11 15:36

You can use this:

myDropDown.Items.Remove(myDropDown.Items.FindByValue("TextToFind"));
查看更多
唯我独甜
4楼-- · 2019-02-11 15:37

I have Done Like this, i have remove all items except the value coming as 1 and 3.

ListItemCollection liCol = ddlcustomertype.Items;
for (int i = 0; i < liCol.Count;i++ )
{
    ListItem li = liCol[i];
    if (li.Value != "1" || li.Value != "3")
        ddlcustomertype.Items.Remove(li);
}
查看更多
SAY GOODBYE
5楼-- · 2019-02-11 15:44

There is also a slightly simpler way of removing the value.

mydropdownid.Items.Remove("Chicago"); 
<dropdown id=mydropdown .....>

values

  • Florida
  • Texas
  • Utah
  • Chicago
查看更多
Rolldiameter
6楼-- · 2019-02-11 15:45

As other people have answered, you need to do;

myDropDown.Items.Remove(ListItem li);

but if you want the page to refresh asynchronously, the dropdown needs to be inside an asp:UpdatePanel

after you do the Remove call, you need to call:

yourPanel.Update();
查看更多
乱世女痞
7楼-- · 2019-02-11 15:50

Code:

ListItem removeItem= myDropDown.Items.FindByValue("TextToFind");
drpCategory.Items.Remove(removeItem);

Replace "TextToFind" with the item you want to remove.

查看更多
登录 后发表回答