Remove an item by index from DropDownList in asp.net c#


DropDownList Items.RemoveAt() method

Dropdownlist control hold a collection of list items. User can select a single item from dropdownlist. Each list item can have display text and it's associated value.

We can remove dropdownlist list item from it's item collections.

Dropdownlist RemoveAt method (function) allow us to remove any item from collection by it's index number.

Dropdownlist server control's list item index start from zero (0). So if you want to remove first item from dropdownlist then pass the index value 0 to the RemoveAt function or you want to delete any other item from dropdownlist then pass the specific index number to the RemoveAt method .

The following example show you how can we remove (delete) dropdownlist list item from specific index position by it's index number.

C# Code

  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;

namespace asptutorial
{
    public partial class blankpage : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            DropDownList1.Items.Add("Sachin");
            DropDownList1.Items.Add("Yadav");
            DropDownList1.Items.Add("Deepak");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int myNumber = Convert.ToInt32(TextBox1.Text);
            DropDownList1.Items.RemoveAt(myNumber);
            Label1.Text = "Item removed from index number: " + TextBox1.Text; 

        }
    }
}

HTML Code

<%@ Page Language="C#" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e) {  
        int myNumber = Convert.ToInt32(TextBox1.Text);  
        DropDownList1.Items.RemoveAt(myNumber);  
        Label1.Text = "Item removed from index number: " + TextBox1.Text;  
    }     
</script>  

<html>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <asp:Label ID="Label1" runat="server" Font-Bold="true" ForeColor="BlueViolet">  
        <br />  
        <asp:DropDownList ID="DropDownList1" runat="server">  
            <asp:ListItem>Sachin</asp:ListItem>  
            <asp:ListItem>Yadav</asp:ListItem>  
            <asp:ListItem>Deepak</asp:ListItem>  
        </asp:DropDownList>  
        <br />
        <b>Index start from 0</b>  
        <br />  
        <asp:Label ID="Label2" runat="server" Text="Position[Index]" AssociatedControlID="TextBox1">  
        <asp:TextBox ID="TextBox1" runat="server">  
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" Text="*">  
        <br />  
        <asp:Button ID="Button1" runat="server" Text="Remove Item" OnClick="Button1_Click" />  
    </div>  
    </form>  
</body>  
</html>  

Share this article on