[c#] asp.net: How can I remove an item from a dropdownlist?

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?

This question is related to c# asp.net drop-down-menu

The answer is


You can use this:

myDropDown.Items.Remove(myDropDown.Items.FindByValue("TextToFind"));

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);

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"));

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

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("Chicago"));


myDropDown.Items.Remove(myDropDown.Items.FindByText("TextToFind"))

I would add an identifying Id or class to the dropbox and remove using Javascript.

The article here should help.

D


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();

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);
}

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to asp.net

RegisterStartupScript from code behind not working when Update Panel is used You must add a reference to assembly 'netstandard, Version=2.0.0.0 No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization How to use log4net in Asp.net core 2.0 Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state How to create roles in ASP.NET Core and assign them to users? How to handle Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause() ASP.NET Core Web API Authentication Could not load file or assembly 'CrystalDecisions.ReportAppServer.CommLayer, Version=13.0.2000.0 WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery How to implement drop down list in flutter? How can I create a dropdown menu from a List in Tkinter? How can I close a dropdown on click outside? Making a drop down list using swift? HTML: Select multiple as dropdown How to get selected value of a dropdown menu in ReactJS Avoid dropdown menu close on click inside Bootstrap 3 dropdown select How to make a drop down list in yii2? Android custom dropdown/popup menu