Dropdownlist value remove in Listbox

N

NBercasio

My dropdown and listbox got the same values. If I choose a value in my
dropdown, i want this value to be removed from my listbox.

So far I have this but it is not working...

CODE BEHIND:

Public Sub ddldeptowner_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
ddldeptowner.SelectedIndexChanged
Dim LI As ListItem
Dim index As String
index = ddldeptowner.SelectedItem.Value
For Each LI In lstboxadddept.Items
lstboxadddept.Items.Remove(index)
Next
End Sub

WEBFORM:

<asp:dropdownlist Runat="server" ID="ddldeptowner" AutoPostBack=True
OnSelectedIndexChanged="ddldeptowner_SelectedIndexChanged"></asp:dropdownlist>
 
S

Scott

What you are doing will remove all of the items from the drop down; instead of looping over the items, try:

lstboxadddept.Items.RemoveAt(index)

And get rid of the For Each LI In... you can also call lstboxaddept.Items.FindByValue(ddldeptowner.SelectedItem.Value) to find the particular item you are interested in.

Scott
My dropdown and listbox got the same values. If I choose a value in my
dropdown, i want this value to be removed from my listbox.

So far I have this but it is not working...

CODE BEHIND:

Public Sub ddldeptowner_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
ddldeptowner.SelectedIndexChanged
Dim LI As ListItem
Dim index As String
index = ddldeptowner.SelectedItem.Value
For Each LI In lstboxadddept.Items
lstboxadddept.Items.Remove(index)
Next
End Sub

WEBFORM:

<asp:dropdownlist Runat="server" ID="ddldeptowner" AutoPostBack=True
OnSelectedIndexChanged="ddldeptowner_SelectedIndexChanged"></asp:dropdownlist>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top