I have a dropdownlist, and in some cases need to remove an item (in the code-behin开发者_如何学God). I need to remove the item based on the value of the item.
How can I do this?
You can use this:
myDropDown.Items.Remove(myDropDown.Items.FindByValue("TextToFind"));
Code:
ListItem removeItem= myDropDown.Items.FindByValue("TextToFind");
drpCategory.Items.Remove(removeItem);
Replace "TextToFind" with the item you want to remove.
myDropDown.Items.Remove(myDropDown.Items.FindByText("TextToFind"))
You can use the
myDropDown.Items.Remove(ListItem li);
or
myDropDown.Items.RemoveAt(int index);
to remove it using C#.
There is also a slightly simpler way of removing the value.
mydropdownid.Items.Remove("Chicago");
<dropdown id=mydropdown .....>
values
- Florida
- Texas
- Utah
- Chicago
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();
myDropDown.Items.Remove(myDropDown.Items.FindByText("Chicago"));
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);
}
Try this code.
If you can add any item and set value in dropdown then try it.
dropdown1.Items.Insert(0, new ListItem("---All---", "0"));
You can Removed Item in dropdown then try it.
ListItem removeItem = dropdown1.Items.FindByText("--Please Select--");
dropdown1.Items.Remove(removeItem);
I would add an identifying Id or class to the dropbox and remove using Javascript.
The article here should help.
D
to insert into DropDownList:
DropDownList.Items.Insert(0, new ListItem("-- Select item --", "0"));
and to remove item from DropDownList:
DropDownList.Items.Remove(new ListItem("-- Select item --", "0"));
精彩评论