In this tutorial we will learn how to use ListBox in or what is ListBox in or how to get ListBox selected items in with example.

Description: ListBox control is used to create a single- or multi-selection drop-down list. The ListBox control is similar to the DropDownList control with two main differences. First, the ListBox control requires more screen real estate because it always displays a certain number of list items. Second, The ListBox control enables a user to select multiple items by setting the SelectionMode property to ListSelectionMode.Multiple. When it is rendered on the page, it is implemented through <select/> HTML tag.

The ListBox control also supports data binding. To bind the control to a data source, first create a data source, such as a System.Collections.ArrayList, that contains the items to display in the control. Next, use the Control.DataBind method to bind the data source to the ListBox control. Use the DataTextField and DataValueField properties to specify which field in the data source to bind to the Text and Valueproperties, respectively, of each list item in the control. The ListBox control will now display the information from the data source.



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListBox.aspx.cs"
Inherits="AspDotNet.ListBox" %>
<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
            <div><b>DropDownList Demo</b></div>
                    <td>SelecSelect as Option:</td>
                        <asp:ListBox ID="ListBox1" AutoPostBack="true" runat="server"
                    Width="150" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
                    <td colspan="2"><b>Selected Item:</b></td>
                    <td>Item Value: </td>
                  <asp:Label ID="itemvalue" runat="server" Text="Label"></asp:Label>
                    <td>Item Text: </td>
                        <asp:Label ID="itemtext" runat="server" Text="Label"></asp:Label>


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AspDotNet
    public partial class ListBox : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
                ArrayList list = new ArrayList();
                list.Add(new ListItem("Item1", "I001"));
                list.Add(new ListItem("Item2", "I002"));
                list.Add(new ListItem("Item3", "I003"));
                list.Add(new ListItem("Item4", "I004"));
                list.Add(new ListItem("Item5", "I005"));
                ListBox1.DataSource = list;
                ListBox1.DataTextField = "Text";
                ListBox1.DataValueField = "Value";
        }               protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
            itemtext.Text = ListBox1.SelectedItem.Text;
            itemvalue.Text = ListBox1.SelectedItem.Value;

Above example when you change selected item then called SelectedIndexChanged event where you can get selected value and text.

Let’s see output:

You can add its option items by directly writing into .aspx page directly or dynamically add at run time or bind through database.

Following are some important properties that are very useful.





No. of rows (items) can be set to display in the List.


Single or Multiple. If multiple, it allows user to select multiple items from the list by holding Ctrl or Shift key.


Get the value of the Selected item from the dropdown box.


Gets or Sets the index of the selected item in the dropdown box.


Gets the selected item from the list.


Gets the collection of items from the dropdown box.


Name of the data source field to supply the text of the items. (No need to set when you are adding items directly into .aspx page.)


Name of the data source field to supply the value of the items. (No need to set when you are adding items directly into .aspx page.)


ID of the datasource component to provide data. (Only used when you have any DataSource component on the page, like SqlDataSource, AccessDataSource etc.)


The datasource that populates the items in the listbox box. (Generally used when you are dynamically generating the items from Database.)


true or false. If true, the form is automatically posted back to the server when user changes the dropdown list selection. It will also fire OnSelectedIndexChanged method.


true or false. If true, the statically added item (added from .aspx page) is maintained when adding items dynamically (from code behind file) or items are cleared.


Method name that fires when user changes the selection of the dropdown box. (Fires only when AutoPostBack=true.)


I hope this will help to you.

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like