Introduction:
ListBox is the popular control used in ASP.NET. ListBox is a list of items which are added by value and text. This article will cover the following points:
- Display Data in ListBox
- Add Items in ListBox
- Delete Items from ListBox
To demonstrate ListBox functionality we will use the following XML Data.
XML Data:
Listing 1: XML Data
<?xml version="1.0" standalone="yes"?> <Browsers> <Browser> <ID>1</ID> <Name>Internet Explorer</Name> </Browser> <Browser> <ID>2</ID> <Name>Chrome</Name> </Browser> <Browser> <ID>3</ID> <Name>Mozilla Firefox</Name> </Browser> </Browsers>

Figure 1: ListBox with the Original XML Data
ASP.NET Script:
The ListBox is added in ASP.NET script. The page contains the one ListBox and three buttons for Add, Delete and Repopulate the ListBox items.
Listing 2: ASP.NET Script with Controls
<head runat="server"> <title>MrBool.com Tutorials - ListBox</title> </head> <body> <form id="form1" runat="server"> <div> <b>ListBox Demo</b> <br /> <br /> <asp:ListBox ID="lstBrowser" runat="server" Width="200px"></asp:ListBox> <br /> <br /> </div> <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="Add " /> <asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Delete" /> <asp:Button ID="btnRePopulate" runat="server" OnClick="btnRePopulate_Click" Text="Re-Populate" /> </form> </body>
C# Code:
Let’s discuss the C# code to add, delete and repopulate the items in ListBox.
Add ListBox Items in C#:
To add an item in ListBox first create a ListItem by passing the value and text and then add this item to our ListBox. After adding the item you can change the selection to newly added item to make it more interactive.

Figure 2: Add Item - ListBox is added with one item Safari-4
Listing 3: C# Code to Add Items in ListBox
protected void btnAdd_Click(object sender, EventArgs e) { int NewIndex = lstBrowser.Items.Count + 1; ListItem lstItems = new ListItem("Safari-" + NewIndex.ToString(), NewIndex.ToString()); // Change the DataSource. lstBrowser.Items.Add(lstItems); lstBrowser.SelectedIndex = NewIndex-1; }
Delete ListBox Items in C#:
To delete an item from ListBox first find out the SelectedIndex and then use the RemoveAt method to delete the item. You need to be extra careful while deleting the items because in case of no items or wrong index the program will through exception. So add the conditions like below.

Figure 3: Delete Item - One item is deleted from ListBox - “Internet Explorer”
Listing 4: C# Code to Delete Items in ListBox
protected void btnDelete_Click(object sender, EventArgs e) { if (lstBrowser.Items.Count > 0 && lstBrowser.SelectedIndex >= 0) { lstBrowser.Items.RemoveAt(lstBrowser.SelectedIndex); if (lstBrowser.Items.Count > 0) lstBrowser.SelectedIndex = 0; } }
Repopulate ListBox Items:
To repopulate items in Listbox you have to get the dataset of items and then assign text and value field. Now assign the dataset and bind it.

Figure 4: Repopulate Item - ListBox with original values after repopulation
Listing 5: C# Code to Repopulate Items in ListBox
protected void btnRePopulate_Click(object sender, EventArgs e) { DataSet oDs = ReadDataSet(); lstBrowser.DataTextField = "Name"; lstBrowser.DataValueField = "ID"; lstBrowser.DataSource = oDs; lstBrowser.DataBind(); }