ObjectDataSource / MultiSelect ListBox

Discussion in 'ASP .Net Web Controls' started by Jay Pondy, Sep 22, 2007.

  1. Jay Pondy

    Jay Pondy Guest

    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.
    Jay Pondy, Sep 22, 2007
    #1
    1. Advertising

  2. Jay Pondy

    Jay Pondy Guest

    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 <> wrote:

    >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.
    Jay Pondy, Sep 22, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JIK

    multiselect dropdown

    JIK, Feb 6, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    8,088
    PeterBrunone
    May 13, 2009
  2. Lenard Gunda
    Replies:
    3
    Views:
    1,240
    =?Utf-8?B?S2FubmFuLlYgW01DU0QubmV0XQ==?=
    Aug 17, 2005
  3. Jeff Thur
    Replies:
    2
    Views:
    194
    Kostadin Kostov
    Feb 14, 2005
  4. Martin

    Unexpected behaviour of Listbox MultiSelect

    Martin, Oct 18, 2005, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    328
    Steven Cheng[MSFT]
    Jan 24, 2006
  5. Nick Nelson

    Listbox Multiselect as filter for Gridview

    Nick Nelson, Feb 28, 2007, in forum: ASP .Net Web Controls
    Replies:
    8
    Views:
    1,134
    chiefsss
    Aug 24, 2009
Loading...

Share This Page