ListBox

 Rate It (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 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 property to "true".  When this property is set to true, the existing ListItems are not removed and the items that are databound will be appended.

<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;

 

 

Revision number 5, Wednesday, February 27, 2008 12:03:34 AM by mbanavige

Comments

Related Articles

Bind ListControl's DataValueField/TextField to multiple columns

Concept A very common question asked on the ASP.NET Forums is how to bind a ListControl's DataTextField or DataValueField to multiple Columns. The ListControl is the base class for controls such as DropDownList, ListBox, RadioButtonList, and CheckBoxList

Using Extension methods in ASP.NET 3.0 and 3.5 with generics.

Since the dawn of asp.net, webbased development has becomea lot easier. It provides a great deal of control over designs and code. And affords the developer morepower over his/her business logic; ratherthan becoming bogged down with the roughness of using

Web Controls

Web controls are ASP.NET controls which are understood and compiled on the server and render as HTML markup on the page. Most of the web server controls render their own HTML controls (or many HTML controls). The markup that is output froma web control can

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. bmains (13)
  2. XIII (13)
  3. vik20000in (8)
  4. mbanavige (7)
  5. anas (5)
  6. tmorton (5)
  7. k_nitin_r (3)
  8. binoj7 (3)
  9. scott@elbandit.co.uk (2)
  10. sciguy65 (1)

Advertise Here