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
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" <fabio.visin@community.nospam>
>Subject: Bind data in a ListBox with multiple selectionmode
[quoted text clipped - 43 lines]
>Thanks
>Fabio
Fabio Visin - 21 Feb 2008 09:18 GMT
Perfect!!!
Really thanks Steven!!!
> Hi Fabio,
>
[quoted text clipped - 136 lines]
>>Thanks
>>Fabio
Steven Cheng - 21 Feb 2008 09:33 GMT
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" <fabio.visin@community.nospam>
>References: <OT9Xvh7cIHA.148@TK2MSFTNGP04.phx.gbl>
<SJTj3LGdIHA.6844@TK2MSFTNGHUB02.phx.gbl>
>Subject: Re: Bind data in a ListBox with multiple selectionmode
>Date: Thu, 21 Feb 2008 10:18:33 +0100
[quoted text clipped - 71 lines]
>>
>> Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
>> ications.
>>
[quoted text clipped - 67 lines]
>>>Thanks
>>>Fabio