Selecting from a List

ASP.NET provides four ASP controls for selecting either single or multiple items from a list:

CheckBoxList
RadioButtonList
ListBox
DropDownList

All of these controls are derived from ListControl and have much in common:

  • ListItem (the information displayed by the list) works exactly the same way for all the ListControls, with a Value property and a Text property.

  • Items can be added either statically, programmatically through the Add method, or from a data source.

  • The SelectedIndex and SelectedItem properties point to the selected item with the lowest index.

  • The Selected property is true for any item that is selected.

  • All four controls raise and respond to the SelectedIndexChanged event.

The ListBox and DropDownList controls differ from the other two list controls (CheckBoxList and RadionButtonList) in that they appear to the user to be a single control (a list box or a drop-down list) rather than a collection of buttons or checkboxes. The ListBox and DropDownList controls lend themselves to longer lists because they scroll.

Table 5-5 summarizes the differences among the four list controls.

Table 5-5. Differences among the four list controls

Characteristic

CheckBoxList

RadioButtonList

DropDownList

ListBox

Single selection only

x

x

Able to select one or more items

x

x

Displays the entire list

x

x

Displays single item at a time, along with a button for seeing the entire list, using vertical scroll bar if necessary

x

Displays multiple ...

Get Programming ASP.NET, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.