ListBox In DetailsView - Selected Value Not changing in loop

G

Guest

I have a listbox inside of a DetailsView. There are three departments to
choose from, Corporate, Group, Vacation. The listbox is set to
SelectionMode="multiple". I think I have this loop setup correctly, the
problem is as it is looping through it outputs the same value even though
watching it validate each value - false, true, true - it repeats the value
for the first "true" item it comes across.
For instance if I choose Group and Vacation, it validates as false, true,
true, but outputs - Group, Group,

Here is my code:

Protected Sub CheckRequestDetailsView_ItemInserting(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles
CheckRequestDetailsView.ItemInserting

Dim DepartmentListBox As ListBox

DepartmentListBox = CheckRequestDetailsView.FindControl("DepartmentListBox")

Dim DepartmentString As String = ""


Dim Item As ListItem

For Each Item In DepartmentListBox.Items

If Item.Selected Then
DepartmentString += DepartmentListBox.SelectedItem.ToString +

", "

End If

Next
CheckRequestSqlDataSource.InsertParameters(

"DepartmentsCharged").DefaultValue = DepartmentString

End Sub
 
G

Guest

because you are using DepartmentListBox.SelectedItem instead of the Item
which will give you the first item selected in the list repeated by as many
selected items as there are there. Try this instead:

For Each Item In DepartmentListBox.Items
If Item.Selected Then
DepartmentString += Item.Text & ", "
End If
Next
 
G

Guest

Thank you so much!! That was exactly it.

Greg

Phillip Williams said:
because you are using DepartmentListBox.SelectedItem instead of the Item
which will give you the first item selected in the list repeated by as many
selected items as there are there. Try this instead:

For Each Item In DepartmentListBox.Items
If Item.Selected Then
DepartmentString += Item.Text & ", "
End If
Next
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
 

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,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top