Share via


ListBox Web Server Control

Creates a single-selection or multiselection list box.

<asp:ListBox id="Listbox1" 
     DataSource="<% databindingexpression %>"
     DataTextField="DataSourceField"
     DataValueField="DataSourceField"
     AutoPostBack="True|False"
     Rows="rowcount"
     SelectionMode="Single|Multiple"
     OnSelectedIndexChanged="OnSelectedIndexChangedMethod"
     runat="server">

   <asp:ListItem value="value" selected="True|False">
      Text
   </asp:ListItem>

</asp:ListBox>

Remarks

Use the ListBox control to create a list control that allows single or multiple item selection. Use the Rows property to specify the height of the control. To enable multiple item selection, set the SelectionMode property to ListSelectionMode.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.

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 Value properties, respectively, of each list item in the control. The ListBox control will now display the information from the data source.

If the SelectionMode property is set to ListSelectionMode.Multiple, 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 ListSelectionMode.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.

For detailed information on the ListBox Web server control's properties and events, see the ListBox Class documentation.

Example

The following example demonstrates how to use ListBox control to display a list of predefined options to the user. The item chosen by the user is displayed in a Label control.

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      Sub SubmitBtn_Click(sender As Object, e As EventArgs)
         If ListBox1.SelectedIndex > - 1 Then
            Label1.Text = "You chose: " & ListBox1.SelectedItem.Text
         End If
      End Sub 'SubmitBtn_Click
   </script>
</head>
<body>
   <h3>ListBox Example</h3>
   <p>
   <form runat="server">
      <asp:ListBox id="ListBox1" 
           Rows="6"
           Width="100px"
           SelectionMode="Single" 
           runat="server">
         <asp:ListItem>Item 1</asp:ListItem>
         <asp:ListItem>Item 2</asp:ListItem>
         <asp:ListItem>Item 3</asp:ListItem>
         <asp:ListItem>Item 4</asp:ListItem>
         <asp:ListItem>Item 5</asp:ListItem>
         <asp:ListItem>Item 6</asp:ListItem>
      </asp:ListBox>
      <asp:button id="Button1"
           Text="Submit" 
           OnClick="SubmitBtn_Click" 
           runat="server" />
      <p>
      <asp:Label id="Label1" 
           Font-Name="Verdana" 
           Font-Size="10pt" 
           runat="server"/>
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      void SubmitBtn_Click(Object sender, EventArgs e) 
      {
         if (ListBox1.SelectedIndex > -1)
            Label1.Text="The first item you chose: " +
                        ListBox1.SelectedItem.Text;
      }
   </script>
</head>
<body>
   <form runat="server">
      <h3>ListBox Example</h3>
      <asp:ListBox id="ListBox1"  
           Rows="4" 
           SelectionMode="Multiple" 
           Width="100px" 
           runat="server">
         <asp:ListItem>Item 1</asp:ListItem>
         <asp:ListItem>Item 2</asp:ListItem>
         <asp:ListItem>Item 3</asp:ListItem>
         <asp:ListItem>Item 4</asp:ListItem> 
         <asp:ListItem>Item 5</asp:ListItem> 
         <asp:ListItem>Item 6</asp:ListItem>
      </asp:ListBox>
      <asp:button id="Button1"
           Text="Submit" 
           OnClick="SubmitBtn_Click" 
           runat="server" />
      <p>
      <asp:Label id="Label1" 
           Font-Name="Verdana" 
           Font-Size="10pt" 
           runat="server"/>
   </form>
</body>
</html>

See Also

Web Server Controls | ListBox Class