Standard Web server list boxes only let the user select one item at a time.
However, if you set the list box's
SelectionMode property to
the list box will support multiple selections. You can make multiple selections
in list boxes the same way as in other Web controls—you can use the Shift
key with the mouse to select a range of items or the Ctrl key to select multiple
items, clicking one after the other.
You can see a multiple selection list box in the MultiSelectListBox example in the code for the book in Figure 14.5. The user can select multiple items in the list box, click the Submit button, and the program will display her selections in a multiline text box, as you see in the figure.
The MultiSelectListBox example.
The HTML for the multiple-selection list box in Figure 14.5 is almost the
same as for the single-selection list box you saw earlier, except for the addition
multiple attribute. This attribute is a standalone HTML attribute,
which means that you don't have to assign it a value—but, following
the lead of the W3C XHTML specification, Visual Basic assigns it the value "multiple" to
make the HTML more compatible with XML, which does not support standalone attributes:
<select name="ListBox1" size="4" multiple="multiple"
103; LEFT: 158px; POSITION: absolute; TOP: 49px">
<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>
|Tip - XHTML is a reformulation of HTML 4 in XML 1.0. We're not going to work with XHTML much in this book, but if you're interested, take a look at the XHTML specification at http://www.w3.org/TR/xhtml1/.|
When the user makes a new selection in a multiple-selection list box, a
occurs. You don't usually send the page back to the server each time the
user selects a new item in a multiple selection list box, however; instead,
you normally use a Submit button and read the list box selections in the button's
To determine which items are selected in a multiple selection list box, you
can loop over the
Items collection of
ListItem objects, checking
Selected property to see if that item is selected.
Here's how that works in MultiSelectListBoxes example:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "You selected: " & ControlChars.CrLf
For intLoopIndex As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(intLoopIndex).Selected Then
TextBox1.Text &= ListBox1.Items(intLoopIndex).Text & _
That's all you need to handle multiple selections in Web server list boxes.