Multiple selections using the ListBox. SelectionMode
(Page 4 of 5 )
We can enable multiple selections using the ListBox.SelectionMode property which accepts values from the ListSelectionMode enumeration. The enumeration has two values, single and multiple. Let's see an example. First, enable Multiple Selection through the Properties Window as shown below.

Add a Label control to the page. Now the code looks like this:
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True"
Height="125px" Width="125px"
SelectionMode="Multiple"></asp:ListBox>
<br /><br />
<asp:Label ID="Label1" runat="server"
EnableViewState="False"></asp:Label>
And here is the code for the Page_Load() event handler method that we need to run the page:
protected void Page_Load(object sender, EventArgs e)
{
// checking if this page is loading for the first item or it's
// postback. If it's not a postback and the page is loading for
// the first time then we populate the ListBox control with the
// Listitem objects and if it's a PostBack we simply do nothing
if (!IsPostBack)
{
// using a for statement to add ListItem objects to the ListBox control
for (int i = 1; i <= 10; i++)
{
ListItem item = new ListItem();
item.Text = "Item Number " + i.ToString();
item.Value = i.ToString();
ListBox1.Items.Add(item);
}
}
// looping through the ListBox.Items collection property
// to get the value of every ListItem.Selected property
// if it's true we will display that ListItem.Text in the Label Control
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
Label1.Text += "<br />" + item.Text;
}
}
If you run the page you will be able to select multiple items by holding down the Shift Key and selecting the items. Each time the page posts back and the Label control prints out the selected ListItems (through using the ListItem.Text property).

In the page_Load() event handler we use a for statement to populate the ListBox control with items. Inside each iteration we instantiate a ListItem object and assign its Text property through concatenating the string "Item Number" to the value of the variable i. We also have assigned the value of the variable i to the ListItem.Value property. Before we end the iteration we add the ListItem to the ListBox.Items collection property through the use of ListBox.Items.Add() method.
We have placed this code inside an if statement that checks if this page is a post back or not. I think that by now you understand that we place code that populates controls inside an if statement that checks for the IsPostback property, because we depend on the ASP.NET ViewState capabilities to preserve the control's state across multiple postbacks.
The next block is the foreach statement. It is used to loop through a collection of objects. The object in a specific iteration is available inside the block. We simply check the value of the Selected property, which returns true or false, for every ListItem object in the ListBox.Items collection property. If it's true then this means that this ListItem is selected; if so we add the ListItem.Text property's value to Label.Text property. Also note that we have disabled the ViewState on the Label control because if we didn't disable it we would get a big string value in the Label.Text property that contains the selected items from this postback and for all the previous postbacks.
The following screen shot is taken before disabling the ViewState of the Label control. By selecting only the first three items we have all these entries in the Label.Text property.

You can also connect the ListBox control to a database to retrieve data. You can do this through ADO.NET code or by using a DataSource control like the SqlDataSource control.