search this blog [2700+ asp.net c# examples]

Loading...

Remove DropDownList list item in asp.net c#

asp.net DropDownList: how to remove list item programmatically
DropDownListRemoveItem.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownListRemoveItem.aspx.cs" Inherits="DropDownListRemoveItem" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net DropDownList: how to remove list item programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">DropDownList: Remove List Item</h2>
        <asp:Label 
            ID="Label1"
            runat="server"
            Font-Bold="true"
            ForeColor="Tomato"
            >
        </asp:Label>
        <br />
        <asp:DropDownList 
            ID="DropdownList1"
            runat="server"
            ForeColor="AliceBlue"
            BackColor="CornflowerBlue"
            >
            <asp:ListItem>Button</asp:ListItem>
            <asp:ListItem>CheckBox</asp:ListItem>
            <asp:ListItem>CheckBoxList</asp:ListItem>
            <asp:ListItem>DropDownList</asp:ListItem>
        </asp:DropDownList>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            Text="Remove Item"
            OnClick="Button1_Click"
            ForeColor="CornflowerBlue"
            Font-Bold="true"
            />
    </div>
    </form>
</body>
</html>
DropDownListRemoveItem.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class DropDownListRemoveItem : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e) {
        string txt = DropdownList1.SelectedItem.Text.ToString();
        DropdownList1.Items.Remove(DropdownList1.SelectedItem);
        Label1.Text = "Item removed: " + txt;
    }
}












Related asp.net example