B
Bob Malcoprs
Hi,
I want to get all the selected values of a listbox in several labels..
With this code here below, when i click on e.g. "option 2", i get '2' in
label2. That's ok.
But if i then click on "option 3", "option 3" is selected and "option 2" is
deselected. So i get '3' in label3 but also still '2' in label2. Same with
"option 1".
And when i deselect everything, i still get '2' and '3'. I can't get rid of
them.
When selecting "option 3" and so deselecting "option2", this line should be
False:
" If ListBox1.Items(1).Selected = True" => should not execute "Label2.Text =
ListBox1.Items(1).Value".
So why does that happen?
How to get only the selected values?
Thanks for help
Bob
aspx file:
<div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack=true
SelectionMode=Multiple>
<asp:ListItem Value=1 Text="option 1"></asp:ListItem>
<asp:ListItem Value=2 Text="option 2"></asp:ListItem>
<asp:ListItem Value=3 Text="option 3"></asp:ListItem>
</asp:ListBox>
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Label ID="Label2" runat="server"></asp:Label>
<asp:Label ID="Label3" runat="server"></asp:Label>
code-behind:
Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles
ListBox1.SelectedIndexChanged
If ListBox1.Items(0).Selected = True Then Label1.Text =
ListBox1.Items(0).Value
If ListBox1.Items(1).Selected = True Then Label2.Text =
ListBox1.Items(1).Value
If ListBox1.Items(2).Selected = True Then Label3.Text =
ListBox1.Items(2).Value
End Sub
I want to get all the selected values of a listbox in several labels..
With this code here below, when i click on e.g. "option 2", i get '2' in
label2. That's ok.
But if i then click on "option 3", "option 3" is selected and "option 2" is
deselected. So i get '3' in label3 but also still '2' in label2. Same with
"option 1".
And when i deselect everything, i still get '2' and '3'. I can't get rid of
them.
When selecting "option 3" and so deselecting "option2", this line should be
False:
" If ListBox1.Items(1).Selected = True" => should not execute "Label2.Text =
ListBox1.Items(1).Value".
So why does that happen?
How to get only the selected values?
Thanks for help
Bob
aspx file:
<div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack=true
SelectionMode=Multiple>
<asp:ListItem Value=1 Text="option 1"></asp:ListItem>
<asp:ListItem Value=2 Text="option 2"></asp:ListItem>
<asp:ListItem Value=3 Text="option 3"></asp:ListItem>
</asp:ListBox>
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Label ID="Label2" runat="server"></asp:Label>
<asp:Label ID="Label3" runat="server"></asp:Label>
code-behind:
Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles
ListBox1.SelectedIndexChanged
If ListBox1.Items(0).Selected = True Then Label1.Text =
ListBox1.Items(0).Value
If ListBox1.Items(1).Selected = True Then Label2.Text =
ListBox1.Items(1).Value
If ListBox1.Items(2).Selected = True Then Label3.Text =
ListBox1.Items(2).Value
End Sub