Home All Groups Group Topic Archive Search About

ObjectDataSource / MultiSelect ListBox

Author
22 Sep 2007 5:40 PM
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.

Author
22 Sep 2007 6:18 PM
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


On Sat, 22 Sep 2007 13:40:56 -0400, Jay Pondy <jpo***@bellsouth.net> wrote:

Show quote
>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.

AddThis Social Bookmark Button