Controls You Can Use on Web Forms ASP.NET Standard Controls ListBox Control
The ListBox Web server control allows users to select one or more items from a predefined list. It differs from a DropDownList control in that it can display multiple items at once and it optionally allows the user to select multiple items.
The ListBox control is typically used to display more than one item at once. You can control the look of the list in these ways:
- Number of row items displayed — sets the control to display a specific number of items. If the control contains more items than specified, it displays a vertical scrollbar.
- Height and width — sets the size of the control in pixels. In this case, the control ignores the number of rows you’ve set and displays as many as will fit into the height of the control. Some browsers do not support setting the height and width in pixels and will use the row count setting.
As with other Web server controls, you can use style objects to specify the appearance of the control. For details, see Web Forms Server Controls and CSS Styles.
The ListBox control is actually a container for the list’s items. Each item is a separate object with its own properties:
Property |
Description |
Text |
The text displayed in the list. |
Value |
The value associated with an item. Setting this property allows you to associate a value with a specific item without displaying it. For example, you can set the Text property to the name of a U.S. state and the Value property to its postal abbreviation. |
Selected |
A Boolean value indicating whether the item is selected. If the ListBox is set to allow multiple selection, more than one item can be selected. |
To work with items programmatically, you usually access the Items collection of the ListBox control. Items is a standard collection, in which you can add or delete item objects, clear the collection, and so on. To test which items are selected, you loop through the collection and test individual items.
For ease of use, the ListBox control also supports a SelectedItem property. If the control is set to single-selection mode, this property returns the selected item, without having to loop through the Items collection.
Users can normally select a single item in the list by clicking it. If you set the ListBox control to allow multiple selections, users can hold down the CTRL or SHIFT key while clicking to select multiple items.
You can use a ListBox Web server control to list options that are read from a data source. Each item in the ListBox control corresponds to an item — typically a row — in the data source.
The control displays one field from the source. Optionally, you can bind the control to a second field to set the value ( which does not display ) of an item.
As with other Web server controls, you can bind any control properties, such as the color or size of the control, to data. For details, see Web Forms Data Binding.
The ListBox control raises an event — SelectedIndexChanged — when users select an item. By default, this event does not cause the page to be posted to the server, but you can cause the control to force an immediate post by setting the AutoPostBack property to true.
If the ListBox control has been has been set to allow multiple selection and AutoPostBack is true, the form is posted back to the server with each selection. However, the current selection is maintained during each round trip.
NOTE: The ability of a ListBox control to automatically post to the server when clicked requires that the browser support ECMAScript and that scripting is enabled on the user’s browser.
Adding ListBox Controls to a Web Forms Page