Listbox Selected Text

  • Thread starter Thread starter Big E
  • Start date Start date
B

Big E

I'm using ASP.Net and 1 Listbox and 1 ComboBox.
When a user selects the contents in the listbox I want to populate the
combobox with the text that he selected or double clicked. I've tried
various things with the selecteditem, index properties to no avail.

Meaning: LISTBOX has CAR, DOG, BAT, HAT already populated in it.
When user selects DOG -> I would like to populate the ComboBox with DOG.

I'm writing everything in the vb file in code.

Thanks.
 
Hi E?

Don't forget to set AutoPostBack="True" for the listbox. That will fire the
event in which you can transfer the item. Here's some code in VB:

Private Sub ListBox1_SelectedIndexChanged _
(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
Dim li As ListItem
li = ListBox1.SelectedItem
DropDownList1.Items.Add(li)
DropDownList1.SelectedIndex = -1
ListBox1.Items.Remove(li)
End Sub

<form id="Form1" method="post" runat="server">
<asp:ListBox id="ListBox1" runat="server" AutoPostBack="True">
<asp:ListItem Value="CAR">CAR</asp:ListItem>
<asp:ListItem Value="DOG">DOG</asp:ListItem>
<asp:ListItem Value="BAT">BAT</asp:ListItem>
<asp:ListItem Value="HAT">HAT</asp:ListItem>
</asp:ListBox>
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
</form>

Does this help?

Ken
Microsoft MVP [ASP.NET]
 
Perfecto.

Ken Cox said:
Hi E?

Don't forget to set AutoPostBack="True" for the listbox. That will fire the
event in which you can transfer the item. Here's some code in VB:

Private Sub ListBox1_SelectedIndexChanged _
(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
Dim li As ListItem
li = ListBox1.SelectedItem
DropDownList1.Items.Add(li)
DropDownList1.SelectedIndex = -1
ListBox1.Items.Remove(li)
End Sub

<form id="Form1" method="post" runat="server">
<asp:ListBox id="ListBox1" runat="server" AutoPostBack="True">
<asp:ListItem Value="CAR">CAR</asp:ListItem>
<asp:ListItem Value="DOG">DOG</asp:ListItem>
<asp:ListItem Value="BAT">BAT</asp:ListItem>
<asp:ListItem Value="HAT">HAT</asp:ListItem>
</asp:ListBox>
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
</form>

Does this help?

Ken
Microsoft MVP [ASP.NET]


Big E said:
I'm using ASP.Net and 1 Listbox and 1 ComboBox.
When a user selects the contents in the listbox I want to populate the
combobox with the text that he selected or double clicked. I've tried
various things with the selecteditem, index properties to no avail.

Meaning: LISTBOX has CAR, DOG, BAT, HAT already populated in it.
When user selects DOG -> I would like to populate the ComboBox with DOG.

I'm writing everything in the vb file in code.

Thanks.
 
Back
Top