Web Forms - Working with ASP.NET server controls

Creating Multiple-Selection List Boxes

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 Multiple, 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.

Figure 14.5
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 of the 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"
id="ListBox1" style="height:113px;width:141px;Z-INDEX:
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 SelectedIndexChanged event 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 Click event handler.

To determine which items are selected in a multiple selection list box, you can loop over the Items collection of ListItem objects, checking each item's 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 & _
    End If
End Sub

That's all you need to handle multiple selections in Web server list boxes.

You might also like...



Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“You can stand on the shoulders of giants OR a big enough pile of dwarfs, works either way.”