ListBox In DetailsView - Selected Value Not changing in loop

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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
 
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
 
Back
Top