Bind data in a ListBox with multiple selectionmode

Discussion in 'ASP .Net' started by Fabio Visin, Feb 20, 2008.

  1. Fabio Visin

    Fabio Visin Guest

    I'm working with a DetailsView in edit mode. One of its controls is a
    listbox with selectionmode set to multiple. I retrieve the the data for the
    detailview from a database so I use this code:
    <asp:ListBox ID="ListBoxPaese" runat="server" DataSourceID="PaesiDataSource"

    DataTextField="Extended_name_It" DataValueField="Code2Char" Rows="8"

    SelectionMode="Multiple" SelectedValue='<%# Eval("Paese") %>'>

    </asp:ListBox>

    The value "Paese" (country) in my table is a string with comma to separate
    the countries (i.e. "al" or "al,ba,bg"); in the first case I have no problem
    but when I expect to see more listitem selected, like se second case, I
    obviously get en error.
    I try with a function to elaborate the data before the binding but with no
    result. So my new listbox code is:

    <asp:ListBox ID="ListBoxPaese" runat="server" DataSourceID="PaesiDataSource"

    DataTextField="Extended_name_It" DataValueField="Code2Char" Rows="8"

    SelectionMode="Multiple" SelectedValue='<%#
    CreateListItem(Eval("Paese")) %>'>

    </asp:ListBox>

    Function CreateListItem(ByVal elenco As String) As ListItemCollection
    Dim listItems As New ListItemCollection

    Dim elencoS As String() = Split(elenco, ",")

    For i As Integer = 0 To elencoS.Length - 1
    listItems.Add(elencoS(i))
    Next

    Return listItems
    End Function


    How can I solve my problem?

    Thanks
    Fabio
     
    Fabio Visin, Feb 20, 2008
    #1
    1. Advertising

  2. Fabio Visin

    Steven Cheng Guest

    Hi Fabio,

    Regarding on the problem you met, I think it is due to the MultiSelect mode
    ListBox doesn't support direct databinding of the selected values.

    I think the proper ways to mark all the selected items is as below:

    ** add a Label control(you can set it to Visible="False") and bind the
    comma separated string (selected items) to the Label

    ** in the ListBox (or Label)'s PreRender event, you retrieve the string
    value from Label.Text and then loop through it to set the certain
    ListItem's Selected property. Here is a simple page demonstrate this:


    ===========aspx============
    <div>
    <asp:Label ID="lblHide" runat="server" Text='<%# GetData()
    %>'></asp:Label>
    <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"

    Height="107px" Width="85px" onprerender="ListBox1_PreRender">
    <asp:ListItem Text="Item1" Value="Item1">Item1</asp:ListItem>
    <asp:ListItem Text="Item2" Value="Item2">Item2</asp:ListItem>
    <asp:ListItem Text="Item3" Value="Item3">Item3</asp:ListItem>
    <asp:ListItem Text="Item4" Value="Item4">Item4</asp:ListItem>
    <asp:ListItem Text="Item5" Value="Item5">Item5</asp:ListItem>
    </asp:ListBox>
    </div>


    =======code behind===========
    protected void Page_Load(object sender, EventArgs e)
    {
    lblHide.DataBind();
    }

    protected string GetData()
    {
    return "Item1,Item2";
    }



    protected void ListBox1_PreRender(object sender, EventArgs e)
    {
    ListBox lb = (ListBox)sender;
    Label lbl = (Label)lb.NamingContainer.FindControl("lblHide");
    string value = lbl.Text;

    string[] items = value.Split(new char[] { ',' });

    foreach (string item in items)
    {
    lb.Items.FindByValue(item).Selected = true;
    }

    }
    =============================

    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.




    --------------------
    >From: "Fabio Visin" <>
    >Subject: Bind data in a ListBox with multiple selectionmode
    >Date: Wed, 20 Feb 2008 13:02:10 +0100
    >
    >I'm working with a DetailsView in edit mode. One of its controls is a
    >listbox with selectionmode set to multiple. I retrieve the the data for

    the
    >detailview from a database so I use this code:
    ><asp:ListBox ID="ListBoxPaese" runat="server"

    DataSourceID="PaesiDataSource"
    >
    > DataTextField="Extended_name_It" DataValueField="Code2Char" Rows="8"
    >
    > SelectionMode="Multiple" SelectedValue='<%# Eval("Paese") %>'>
    >
    ></asp:ListBox>
    >
    >The value "Paese" (country) in my table is a string with comma to separate
    >the countries (i.e. "al" or "al,ba,bg"); in the first case I have no

    problem
    >but when I expect to see more listitem selected, like se second case, I
    >obviously get en error.
    >I try with a function to elaborate the data before the binding but with no
    >result. So my new listbox code is:
    >
    ><asp:ListBox ID="ListBoxPaese" runat="server"

    DataSourceID="PaesiDataSource"
    >
    > DataTextField="Extended_name_It" DataValueField="Code2Char" Rows="8"
    >
    > SelectionMode="Multiple" SelectedValue='<%#
    >CreateListItem(Eval("Paese")) %>'>
    >
    ></asp:ListBox>
    >
    > Function CreateListItem(ByVal elenco As String) As ListItemCollection
    > Dim listItems As New ListItemCollection
    >
    > Dim elencoS As String() = Split(elenco, ",")
    >
    > For i As Integer = 0 To elencoS.Length - 1
    > listItems.Add(elencoS(i))
    > Next
    >
    > Return listItems
    > End Function
    >
    >
    >How can I solve my problem?
    >
    >Thanks
    >Fabio
    >
    >
    >
     
    Steven Cheng, Feb 21, 2008
    #2
    1. Advertising

  3. Fabio Visin

    Fabio Visin Guest

    Perfect!!!

    Really thanks Steven!!!


    ""Steven Cheng"" <> ha scritto nel messaggio
    news:...
    > Hi Fabio,
    >
    > Regarding on the problem you met, I think it is due to the MultiSelect
    > mode
    > ListBox doesn't support direct databinding of the selected values.
    >
    > I think the proper ways to mark all the selected items is as below:
    >
    > ** add a Label control(you can set it to Visible="False") and bind the
    > comma separated string (selected items) to the Label
    >
    > ** in the ListBox (or Label)'s PreRender event, you retrieve the string
    > value from Label.Text and then loop through it to set the certain
    > ListItem's Selected property. Here is a simple page demonstrate this:
    >
    >
    > ===========aspx============
    > <div>
    > <asp:Label ID="lblHide" runat="server" Text='<%# GetData()
    > %>'></asp:Label>
    > <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"
    >
    > Height="107px" Width="85px" onprerender="ListBox1_PreRender">
    > <asp:ListItem Text="Item1" Value="Item1">Item1</asp:ListItem>
    > <asp:ListItem Text="Item2" Value="Item2">Item2</asp:ListItem>
    > <asp:ListItem Text="Item3" Value="Item3">Item3</asp:ListItem>
    > <asp:ListItem Text="Item4" Value="Item4">Item4</asp:ListItem>
    > <asp:ListItem Text="Item5" Value="Item5">Item5</asp:ListItem>
    > </asp:ListBox>
    > </div>
    >
    >
    > =======code behind===========
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > lblHide.DataBind();
    > }
    >
    > protected string GetData()
    > {
    > return "Item1,Item2";
    > }
    >
    >
    >
    > protected void ListBox1_PreRender(object sender, EventArgs e)
    > {
    > ListBox lb = (ListBox)sender;
    > Label lbl = (Label)lb.NamingContainer.FindControl("lblHide");
    > string value = lbl.Text;
    >
    > string[] items = value.Split(new char[] { ',' });
    >
    > foreach (string item in items)
    > {
    > lb.Items.FindByValue(item).Selected = true;
    > }
    >
    > }
    > =============================
    >
    > Hope this helps.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    >
    > ==================================================
    >
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications.
    >
    >
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >
    > ==================================================
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    >
    >
    >
    > --------------------
    >>From: "Fabio Visin" <>
    >>Subject: Bind data in a ListBox with multiple selectionmode
    >>Date: Wed, 20 Feb 2008 13:02:10 +0100
    >>
    >>I'm working with a DetailsView in edit mode. One of its controls is a
    >>listbox with selectionmode set to multiple. I retrieve the the data for

    > the
    >>detailview from a database so I use this code:
    >><asp:ListBox ID="ListBoxPaese" runat="server"

    > DataSourceID="PaesiDataSource"
    >>
    >> DataTextField="Extended_name_It" DataValueField="Code2Char" Rows="8"
    >>
    >> SelectionMode="Multiple" SelectedValue='<%# Eval("Paese") %>'>
    >>
    >></asp:ListBox>
    >>
    >>The value "Paese" (country) in my table is a string with comma to separate
    >>the countries (i.e. "al" or "al,ba,bg"); in the first case I have no

    > problem
    >>but when I expect to see more listitem selected, like se second case, I
    >>obviously get en error.
    >>I try with a function to elaborate the data before the binding but with no
    >>result. So my new listbox code is:
    >>
    >><asp:ListBox ID="ListBoxPaese" runat="server"

    > DataSourceID="PaesiDataSource"
    >>
    >> DataTextField="Extended_name_It" DataValueField="Code2Char" Rows="8"
    >>
    >> SelectionMode="Multiple" SelectedValue='<%#
    >>CreateListItem(Eval("Paese")) %>'>
    >>
    >></asp:ListBox>
    >>
    >> Function CreateListItem(ByVal elenco As String) As ListItemCollection
    >> Dim listItems As New ListItemCollection
    >>
    >> Dim elencoS As String() = Split(elenco, ",")
    >>
    >> For i As Integer = 0 To elencoS.Length - 1
    >> listItems.Add(elencoS(i))
    >> Next
    >>
    >> Return listItems
    >> End Function
    >>
    >>
    >>How can I solve my problem?
    >>
    >>Thanks
    >>Fabio
    >>
    >>
    >>

    >
     
    Fabio Visin, Feb 21, 2008
    #3
  4. Fabio Visin

    Steven Cheng Guest

    You're welcome :)

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.


    --------------------
    >From: "Fabio Visin" <>
    >References: <>

    <>
    >Subject: Re: Bind data in a ListBox with multiple selectionmode
    >Date: Thu, 21 Feb 2008 10:18:33 +0100
    >
    >Perfect!!!
    >
    >Really thanks Steven!!!
    >
    >
    >""Steven Cheng"" <> ha scritto nel messaggio
    >news:...
    >> Hi Fabio,
    >>
    >> Regarding on the problem you met, I think it is due to the MultiSelect
    >> mode
    >> ListBox doesn't support direct databinding of the selected values.
    >>
    >> I think the proper ways to mark all the selected items is as below:
    >>
    >> ** add a Label control(you can set it to Visible="False") and bind the
    >> comma separated string (selected items) to the Label
    >>
    >> ** in the ListBox (or Label)'s PreRender event, you retrieve the string
    >> value from Label.Text and then loop through it to set the certain
    >> ListItem's Selected property. Here is a simple page demonstrate this:
    >>
    >>
    >> ===========aspx============
    >> <div>
    >> <asp:Label ID="lblHide" runat="server" Text='<%# GetData()
    >> %>'></asp:Label>
    >> <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"
    >>
    >> Height="107px" Width="85px" onprerender="ListBox1_PreRender">
    >> <asp:ListItem Text="Item1" Value="Item1">Item1</asp:ListItem>
    >> <asp:ListItem Text="Item2" Value="Item2">Item2</asp:ListItem>
    >> <asp:ListItem Text="Item3" Value="Item3">Item3</asp:ListItem>
    >> <asp:ListItem Text="Item4" Value="Item4">Item4</asp:ListItem>
    >> <asp:ListItem Text="Item5" Value="Item5">Item5</asp:ListItem>
    >> </asp:ListBox>
    >> </div>
    >>
    >>
    >> =======code behind===========
    >> protected void Page_Load(object sender, EventArgs e)
    >> {
    >> lblHide.DataBind();
    >> }
    >>
    >> protected string GetData()
    >> {
    >> return "Item1,Item2";
    >> }
    >>
    >>
    >>
    >> protected void ListBox1_PreRender(object sender, EventArgs e)
    >> {
    >> ListBox lb = (ListBox)sender;
    >> Label lbl = (Label)lb.NamingContainer.FindControl("lblHide");
    >> string value = lbl.Text;
    >>
    >> string[] items = value.Split(new char[] { ',' });
    >>
    >> foreach (string item in items)
    >> {
    >> lb.Items.FindByValue(item).Selected = true;
    >> }
    >>
    >> }
    >> =============================
    >>
    >> Hope this helps.
    >>
    >> Sincerely,
    >>
    >> Steven Cheng
    >>
    >> Microsoft MSDN Online Support Lead
    >>
    >>
    >>
    >> ==================================================
    >>
    >> Get notification to my posts through email? Please refer to
    >>

    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    >> ications.
    >>
    >>
    >>
    >> Note: The MSDN Managed Newsgroup support offering is for non-urgent

    issues
    >> where an initial response from the community or a Microsoft Support
    >> Engineer within 1 business day is acceptable. Please note that each

    follow
    >> up response may take approximately 2 business days as the support
    >> professional working with you may need further investigation to reach the
    >> most efficient resolution. The offering is not appropriate for situations
    >> that require urgent, real-time or phone-based interactions or complex
    >> project analysis and dump analysis issues. Issues of this nature are best
    >> handled working with a dedicated Microsoft Support Engineer by contacting
    >> Microsoft Customer Support Services (CSS) at
    >> http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >>
    >> ==================================================
    >>
    >>
    >> This posting is provided "AS IS" with no warranties, and confers no
    >> rights.
    >>
    >>
    >>
    >>
    >> --------------------
    >>>From: "Fabio Visin" <>
    >>>Subject: Bind data in a ListBox with multiple selectionmode
    >>>Date: Wed, 20 Feb 2008 13:02:10 +0100
    >>>
    >>>I'm working with a DetailsView in edit mode. One of its controls is a
    >>>listbox with selectionmode set to multiple. I retrieve the the data for

    >> the
    >>>detailview from a database so I use this code:
    >>><asp:ListBox ID="ListBoxPaese" runat="server"

    >> DataSourceID="PaesiDataSource"
    >>>
    >>> DataTextField="Extended_name_It" DataValueField="Code2Char" Rows="8"
    >>>
    >>> SelectionMode="Multiple" SelectedValue='<%# Eval("Paese") %>'>
    >>>
    >>></asp:ListBox>
    >>>
    >>>The value "Paese" (country) in my table is a string with comma to

    separate
    >>>the countries (i.e. "al" or "al,ba,bg"); in the first case I have no

    >> problem
    >>>but when I expect to see more listitem selected, like se second case, I
    >>>obviously get en error.
    >>>I try with a function to elaborate the data before the binding but with

    no
    >>>result. So my new listbox code is:
    >>>
    >>><asp:ListBox ID="ListBoxPaese" runat="server"

    >> DataSourceID="PaesiDataSource"
    >>>
    >>> DataTextField="Extended_name_It" DataValueField="Code2Char" Rows="8"
    >>>
    >>> SelectionMode="Multiple" SelectedValue='<%#
    >>>CreateListItem(Eval("Paese")) %>'>
    >>>
    >>></asp:ListBox>
    >>>
    >>> Function CreateListItem(ByVal elenco As String) As ListItemCollection
    >>> Dim listItems As New ListItemCollection
    >>>
    >>> Dim elencoS As String() = Split(elenco, ",")
    >>>
    >>> For i As Integer = 0 To elencoS.Length - 1
    >>> listItems.Add(elencoS(i))
    >>> Next
    >>>
    >>> Return listItems
    >>> End Function
    >>>
    >>>
    >>>How can I solve my problem?
    >>>
    >>>Thanks
    >>>Fabio
    >>>
    >>>
    >>>

    >>

    >
    >
    >
     
    Steven Cheng, Feb 21, 2008
    #4
    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. Matthew Louden
    Replies:
    1
    Views:
    21,298
    Hero Ngan
    Nov 13, 2003
  2. mr2_93
    Replies:
    1
    Views:
    3,677
    mr2_93
    Oct 2, 2005
  3. Seb
    Replies:
    0
    Views:
    592
  4. Matthew Louden
    Replies:
    1
    Views:
    222
    Hero Ngan
    Nov 13, 2003
  5. Calendar SelectionMode

    , May 30, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    145
Loading...

Share This Page