The text of the selected item
from the ListBox control is being displayed in a Label control in the Click event
of a Button.
Now, you need to change the selection mode of the ListBox control from the default
"Single" to "Multiple" so as to enable multiple list item selection from the control.
You can do this by changing the SelectionMode property in the design view or in
your .aspx file.
Chapter 2
[ 39 ]
Irrespective of whether the ListBox control's selection mode is set to
"Single" or "Multiple", the Count property of the Items collection of the
control would always return you the total number of list items in
the control.
Now, refer to the following code snippet that shows how you can retrieve the
selected text for multiple selected list items from the ListBox control:
protected void Button1_Click(object sender, EventArgs e)
{
string str = String.Empty;
for (int i = 0; i < ListBox1.Items.Count - 1; i++)
{
if (ListBox1.Items[i].Selected)
str += ListBox1.Items[i].Text+",";
}
str = str.Substring(0,str.LastIndexOf(???,'));
Label1.Text = str;
}
You can select any ListItem in the ListBox control by its Text or by its
Value properties. The following code snippets illustrate how you can
do this.
ListBox1.Items.FindByText("Joydip").Selected = true;
or
ListBox1.
Pages:
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53