As when working with Windows forms, you use list boxes to create a control that allows single or multiple selection of items from a list. For example, you can see a single-selection list box in Figure 14.3 in the ListBox example in the code for this book. When the user clicks an item in the list box, that item appears in the text box at the bottom of the application, as you see in the figure.
Figure 14.3
The ListBox example.
In HTML, list boxes are supported with the HTML <select> control,
and the items in the list box are supported with HTML <option> elements.
For example, here's the HTML that creates the single-selection list box
you see in Figure 14.3:
<select name="ListBox1" size="4" onchange="__doPostBack('ListBox1','')"
language="javascript" id="ListBox1"
style="height:128px;width:145px;Z-INDEX: 102;
LEFT: 156px; POSITION: absolute; TOP: 72px">
<option value="Item 0">Item 0</option>
<option value="Item 1">Item 1</option>
<option value="Item 2">Item 2</option>
<option value="Item 3">Item 3</option>
<option value="Item 4">Item 4</option>
<option value="Item 5">Item 5</option>
<option value="Item 6">Item 6</option>
<option value="Item 7">Item 7</option>
<option value="Item 8">Item 8</option>
<option value="Item 9">Item 9</option>
</select>
Web server list boxes are supported by the System.Web.UI.WebControls.ListBox class;
here's the hierarchy for that class (note that this class inherits the System.Web.UI.WebControls.ListControl class,
as check box lists and radio button lists do):
System.Object
System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.ListControl
System.Web.UI.WebControls.ListBox
You can find the significant public properties of System.Web.UI.WebControls.ListBox objects
in Table 14.4. (This class has no non-inherited methods or events.) Note that
as with other Web server controls, this table does not list the significant
properties, methods, and events this class inherits from the Control and WebControl classes—you
can find them in Tables 12.1 to 12.5. Note that this class also inherits the ListControl class,
and you can find the significant public properties of the ListControl objects
in Table 13.4, and their significant public event in Table 13.5. (The ListControl class
has no non-inherited methods.)
Table 14.4 Significant Public Properties of ListBox Objects
| Property | Means |
|---|---|
Items
|
Returns the collection of items (as ListItem objects) in the
control. |
Rows
|
Returns or sets the number of rows in the list box. |
SelectedIndex
|
Returns or sets the selected item's index. |
SelectedItem
|
Returns the ListItem object corresponding to the selected item. |
SelectionMode
|
Returns or sets the list box's selection mode, single or multiple. |
SelectedValue
|
Returns the value of the Value property of the selected item. |
ToolTip
|
Returns or sets the tool tip text for this list box. |
The major event in Web server list boxes is the SelectedIndexChanged event,
which occurs when the selected item in the list box is changed. Note also,
as you saw in Day 11, that if you want to handle a list box's events immediately,
you must set its AutoPostBack property to True.
Here's an overview of list boxes in code. You use the Rows property
to specify the height of the control. To enable multiple item selection, you
set the SelectionMode property to ListSelectionMode.Multiple.
You can determine the selected item in a single-selection list box using the SelectedItem and SelectedIndex properties.
The SelectedItem property returns the selected item as a ListItem object,
which supports Text, Value, and Selected properties.
The ListControl class's Items property holds a collection
of ListItem objects that you can use to access any item in a list
box. (You saw the ListItem class in Day 13, "Web Forms: Working
with Check Boxes, Radio Buttons, Tables, and Panels"; you can find the
significant public properties of ListItem objects in Table 13.6.)
Comments