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;
}
}
- asp.net ListBox: how to remove list Item programmatically
- asp.net ListBox: how to add List Item programmatically
- asp.net DropDownList: how to add list item programmatically
- asp.net ListBox: how to use Multiple SelectionMode
- asp.net ListBox: how to use
- asp.net DropDownList: how to use
- asp.net Calendar: using foreach loop
- asp.net Calendar: how to use Calendar
- asp.net GridView RowDataBound Event example: how to format specific row
- asp.net GridView CommandField, SelectedRowStyle example: how to use CommandField