ObjectDataSource / MultiSelect ListBox

J

Jay Pondy

I have an ObjectDataSource bound to a Details View using a Custom Object.

The DetailsView contains a multi-select listbox. I have been able to get the
selected items from the listbox into a string array and pass the array to the
insert and update parameters successfully as follows:

Protected Sub dsEmployee_Inserting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles
dsEmployee.Inserting
e.InputParameters("FavoriteColors") = FavoriteColors()
End Sub

Protected Sub dsEmployee_Updating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles
dsEmployee.Updating
e.InputParameters("FavoriteColors") = FavoriteColors()
End Sub


Private Function FavoriteColors() As String()

'Convert the selected colors in the ListBox to a string array
Dim lstColors As ListBox =
DirectCast(dvEmployee.FindControl("lstFavoriteColors"), ListBox)
Dim aColors As New ArrayList

For Each oItem As ListItem In lstColors.Items
If oItem.Selected Then aColors.Add(oItem.Value)
Next

Return aColors.ToArray(Type.GetType("System.String"))

End Function


What I can't seem to figure out is how can I access my CustomObject when it gets
bound to the DetailsView so that I make the multi-select listbox reflect what is
in the CustomObject.
 
J

Jay Pondy

Try the DataBound event you dummy!

Protected Sub dvEmployee_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dvEmployee.DataBound

If dvEmployee.DataItem IsNot Nothing Then

Dim oEmployee As Employee = DirectCast(dvEmployee.DataItem,
Employee)
Dim lstColors As ListBox =
DirectCast(dvEmployee.FindControl("lstFavoriteColors"), ListBox)

For Each oItem As ListItem In lstColors.Items
oItem.Selected = Array.IndexOf(oEmployee.FavoriteColors,
oItem.Value) <> -1
Next


End If

End Sub
 

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

Forum statistics

Threads
473,708
Messages
2,569,346
Members
44,650
Latest member
LuckyVivo

Latest Threads

Top