Remove (Delete) list item by item value in DropDownList
The following asp.net c# example code demonstrate us how can we delete/remove an item from
DropDownList server control programmatically at run time based on the specified item's value.
Each ListItem object have 'Text' property and 'Value' property. We can search a ListItem object based on its value within an items collection by using Collection<T> Class FindByValue(T) method. FindByValue() method return the specified ListItem object if found; otherwise it returns null.
So if we pass the item's value to search for the specified ListItem object then FindByValue() method will return the ListItem object if found. Then we can simply remove the specified ListItem object from DropDownList items collection. Collection<T> Class Remove(T) method allow us to remove a specified ListItem object from DropDownList items.
Each ListItem object have 'Text' property and 'Value' property. We can search a ListItem object based on its value within an items collection by using Collection<T> Class FindByValue(T) method. FindByValue() method return the specified ListItem object if found; otherwise it returns null.
So if we pass the item's value to search for the specified ListItem object then FindByValue() method will return the ListItem object if found. Then we can simply remove the specified ListItem object from DropDownList items collection. Collection<T> Class Remove(T) method allow us to remove a specified ListItem object from DropDownList items.
DropDownListRemoveItemByValue.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
string itemValue = TextBox1.Text.ToString();
if (DropDownList1.Items.FindByValue(itemValue) != null)
{
string itemText = DropDownList1.Items.FindByValue(itemValue).Text;
ListItem li = new ListItem();
li.Text = itemText;
li.Value = itemValue;
Label1.Text = "Item Found and remove: " + itemText;
DropDownList1.Items.Remove(li);
}
else
{
Label1.Text = "Item not Found, Value: " + itemValue;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to remove list item by item value in DropDownList</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Green">DropDownList example: Remove List Item</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Bold="true"
ForeColor="Tomato"
Font-Size="Large"
>
</asp:Label>
<br /><br />
<asp:Label
ID="Label2"
runat="server"
Text="asp.net controls"
Font-Bold="true"
ForeColor="DarkCyan"
>
</asp:Label>
<br />
<asp:DropDownList
ID="DropDownList1"
runat="server"
BackColor="DarkCyan"
ForeColor="FloralWhite"
>
<asp:ListItem Value="1">BehaviorEditorPart</asp:ListItem>
<asp:ListItem Value="2">XmlDataSource</asp:ListItem>
<asp:ListItem Value="3">UpdateProgress</asp:ListItem>
<asp:ListItem Value="4">SiteMapDataSource</asp:ListItem>
<asp:ListItem Value="5">ProxyWebPartManager</asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:Label
ID="Label3"
runat="server"
ForeColor="DarkCyan"
Text="Item Value"
>
</asp:Label>
<asp:TextBox
ID="TextBox1"
runat="server"
BackColor="DarkCyan"
ForeColor="Snow"
>
</asp:TextBox>
<br />
<asp:Button
ID="Button1"
runat="server"
Text="Remove List Item"
Font-Bold="true"
ForeColor="DarkCyan"
OnClick="Button1_Click"
/>
</div>
</form>
</body>
</html>




