Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Listbox with selection mode as multiple.
<asp:ListBox ID="ListBox1" SelectionMode="Multiple"  runat="server">
        <asp:ListItem>one</asp:ListItem>
        <asp:ListItem>two</asp:ListItem>
        <asp:ListItem>thr</asp:ListItem>
        </asp:ListBox>


The following code displays the selected item in Listbox. But how do i display multiple selected items in the listbox.
protected void Button1_Click(object sender, EventArgs e)
    {
// let the array srr be the selected items
        string[] srr = { "one", "thr" };
        foreach (string item in srr)
        {
            ListBox1.SelectedValue = item;
        }
    }

Thanks.
Posted
Comments
senguptaamlan 14-Dec-10 6:31am    
where do you want to display and whats the problem that you are facing doing that...please explain a bit more
test-09 14-Dec-10 6:36am    
I want to show in the listBox itself. Show selected multiple items. I mentioned it in question.

C#
protected void Button1_Click(object sender, EventArgs e)
{
 string[] srr = { "one", "thr" };
 foreach (string item in srr)
 {
 for (int intItem = 0; intItem &lt;= ListBox1.Items.Count - 1; intItem++) {
      if(ListBox1.Items(intItem).Text == item){
            ListBox1.Items(intItem).Selected = true;
      }
    }
}
 
Share this answer
 
v3
Comments
m@dhu 14-Dec-10 6:51am    
Good one.
Check the selected property of each item. Have a look at the example at this link:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.items.aspx[^]

Good luck!
 
Share this answer
 
Comments
test-09 14-Dec-10 6:40am    
The link has nothing to do with my requirement. I want to show the selected items(multiple) in the LISTBOX.
E.F. Nijboer 14-Dec-10 10:33am    
If you look at the example you will find the C# code just below the start of the body. It is quite the same as the code given in the answer of ToniyoJackson. Further is ListBox a descendant of ListControl (obviously) and the methods and properties used are simply inherited.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900