ASP 101 - Active Server Pages 101 multiple_selection.aspx
<%@ Page Language="VB" %>
<script language="VB" runat="server">
	' Declare array to hold team names
	Dim arrTeams(29) As String
	Sub Page_Load(sender As Object, e As EventArgs)
		' Only need to populate array and databind the first time through.
		If Not Page.IsPostBack Then
			' I'm using an array to hold the names to illustrate
			' the databinding.  I could just as easily have filled
			' the ListBox using this syntax:
			' myListBox.Items.Add("Anaheim Angels")
			FillArrayWithTeams()
			myListBox.DataSource = arrTeams
			myListBox.DataBind()
		End If
		' Use the PlaceHolder to hide our selected items messages until
		' the user selects something and clicks the submit button.
		myPlaceHolder.Visible = False
	End Sub
	Sub btnSubmit_OnClick(sender As Object, e As EventArgs)
		Dim Item As ListItem
		Dim intSelectedCount As Integer
		Dim colSelectedItems As New Collection
		' Un-hide our selected items messages
		myPlaceHolder.Visible = True
		' Get count of selected items
		intSelectedCount = 0
		For Each Item In myListBox.Items
			If Item.Selected Then intSelectedCount = intSelectedCount + 1
		Next Item
		litCount.Text = intSelectedCount
		' Create a Collection of the selected items.
		' Note that I could've done this in the loop above while
		' I was doing the count, but I've kept if separate for clarity.
		For Each Item In myListBox.Items
			If Item.Selected Then colSelectedItems.Add(Item)
		Next Item
		' Bind the collection to a DataGrid
		mySelectedItemsDataGrid.DataSource = colSelectedItems
		mySelectedItemsDataGrid.DataBind()
	End Sub
	Sub FillArrayWithTeams()
		arrTeams(0)  = "Anaheim Angels"
		arrTeams(1)  = "Atlanta Braves"
		arrTeams(2)  = "Arizona Diamondbacks"
		arrTeams(3)  = "Baltimore Orioles"
		arrTeams(4)  = "Boston Red Sox"
		arrTeams(5)  = "Chicago Cubs"
		arrTeams(6)  = "Chicago White Sox"
		arrTeams(7)  = "Cincinnati Reds"
		arrTeams(8)  = "Cleveland Indians"
		arrTeams(9)  = "Colorado Rockies"
		arrTeams(10) = "Detroit Tigers"
		arrTeams(11) = "Florida Marlins"
		arrTeams(12) = "Houston Astros"
		arrTeams(13) = "Kansas City Royals"
		arrTeams(14) = "Los Angeles Dodgers"
		arrTeams(15) = "Milwaukee Brewers"
		arrTeams(16) = "Minnesota Twins"
		arrTeams(17) = "Montreal Expos"
		arrTeams(18) = "New York Mets"
		arrTeams(19) = "New York Yankees"
		arrTeams(20) = "Oakland Athletics"
		arrTeams(21) = "Philadelphia Phillies"
		arrTeams(22) = "Pittsburgh Pirates"
		arrTeams(23) = "San Diego Padres"
		arrTeams(24) = "San Francisco Giants"
		arrTeams(25) = "Seattle Mariners"
		arrTeams(26) = "St. Louis Cardinals"
		arrTeams(27) = "Tampa Bay Devil Rays"
		arrTeams(28) = "Texas Rangers"
		arrTeams(29) = "Toronto Blue Jays"
	End Sub
</script>
<html>
<head>
<title>ASP.NET Multiple Selections Sample</title>
</head>
<body>
<form runat="server">
<p>
Pick your favorite teams (hold down CTRL to select more than one):
</p>
<asp:ListBox id="myListBox" runat="server"
	SelectionMode = "Multiple"
	Rows = "10"
/>
<br />
<asp:Button id="btnSubmit" runat="server"
	text="Send Team Selection"
	OnClick="btnSubmit_OnClick"
/>
<br />
<br />
<asp:PlaceHolder id="myPlaceHolder" runat="server">
<p>
You selected <strong><asp:Literal id="litCount" runat="server" /></strong> team(s).
</p>
<p>
Request.Form returned:<br />
<strong><%= Request.Form("myListBox") %></strong>
</p>
<p>
Now just for fun we'll bind a collection of the selected items to a DataGrid:
</p>
<asp:DataGrid id="mySelectedItemsDataGrid" runat="server" />
</asp:PlaceHolder>
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/multiple_selection_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>