Click here to Skip to main content
15,885,216 members
Articles / Data
Article

ListBox

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
11 Oct 2013CPOL2 min read 10.2K   1  
The ListBox control is used to create a list control that allows single or multiple item selection. Use the Rows property to specify the height of

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

The ListBox control is used to create a list control that allows single or multiple item selection. Use the Rows property to specify the height of the control. By default, the ListBox only allows a single selection to be made at a time. To enable the selection of multiple items, set the SelectionMode property to "Multiple".

To specify the items that you want to appear in the ListBox control, place a ListItem element for each entry between the opening and closing tags of the ListBox control.

<asp:ListBox ID="ListBox1" runat="server">

    <asp:ListItem Value="">Please select an item</asp:ListItem>

    <asp:ListItem Value="1">Item 1</asp:ListItem>

    <asp:ListItem Value="2">Item 2</asp:ListItem>

</asp:ListBox>

The ListBox control also supports data binding. To bind the control to a data source, first create a data source, such as one of the DataSourceControl objects, that contains the items to display in the control. Next, use the 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 Value properties, respectively, of each list item in the control. The ListBox control will now display the information from the data source.

By default, when the control is databound, any ListItems currently in the items collection will be removed. Sometimes we might wish to add a single "blank" item with the text "Please select an item".  To insure that this item is not removed during the databinding process, we can set the AppendDataboundItems propert to "true".

<asp:ListBox ID="ListBox2" runat="server" AppendDataBoundItems="true">

    <asp:ListItem Value="">Please select an item</asp:ListItem>

</asp:ListBox>

If the SelectionMode property is set to Multiple, you can determine the selected items in the ListBox control by iterating through the Items collection and testing the Selected property of each item in the collection. If the SelectionMode property is set to Single, you can use the SelectedIndex property to determine the index of the selected item. The index can then be used to retrieve the item from the Items collection.

VB

'test for multiple selections

Dim selectionList As New System.Collections.Generic.List(Of String)

For Each itm As ListItem In ListBox1.Items

    If itm.Selected = True Then

        selectionList.Add(itm.Value)

    End If

Next

 

'get single selection value

Dim selectionValue As String

selectionValue = Me.ListBox1.SelectedValue

 

'or

Dim selectedItem As ListItem

selectedItem = Me.ListBox1.SelectedItem

C#

//test for multiple selections

System.Collections.Generic.List<string> selectionList = new System.Collections.Generic.List<string>();

foreach (ListItem itm in ListBox1.Items)

{

    if (itm.Selected == true)

    {

        selectionList.Add(itm.Value);

    }

}

 

//get single selection value

string selectionValue;

selectionValue = this.ListBox1.SelectedValue;

 

//or

ListItem selectedItem;

selectedItem = this.ListBox1.SelectedItem;

 

 

This article was originally posted at http://wiki.asp.net/page.aspx/332/listbox

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --