Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Working with ListBox in ASP.NET and C# - Add and Delete Items

In this article we will cover the following points: Display Data in ListBox, Add Items in ListBox, Delete Items from ListBox

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>
ListBox with the Original XML Data

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.

Add Item - ListBox is added with one item Safari-4

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.

Delete Item - One item is deleted from ListBox - Internet Explorer

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.

Repopulate Item - ListBox with original values after repopulation

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();
    }


I am having total 7+ years of experience in developing applications using C#, VB.NET, ASP.NET, Silverlight, WCF, SQL Server and Oracle. I am holding a M.Tech degree in Intelligent systems. For more information, visit my Linkedin p...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login