Delete ListBox Items in C#

Akiii
Posted by Akiii under C# category on | Points: 40 | Views : 1434
First lets declare the asp list box control :-

<asp:ListBox ID="lstdemo" runat="server"></asp:ListBox>




We declare an add button to add items in the listbox :-

<asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Add " />



Now on the click of a button, we delete the value to the listbox :-

protected void btnDelete_Click(object sender, EventArgs e)
{
if (lstdemo.Items.Count > 0 && lstdemo.SelectedIndex >= 0)
{
lstdemo.Items.RemoveAt(lstdemo.SelectedIndex);

if (lstdemo.Items.Count > 0)
{
lstBrowser.SelectedIndex = 0;
}
}
}



Thanks and Regards
Akiii

Comments or Responses

Login to post response