Getting Multiple selections from Listbox


Active member
Jul 19, 2019
Programming Experience
I am trying to add the values from multiple line selections from an asp listbox using c# and then add those values to an array but Ive only been able to get single selections out of it. (using Visual Studio 2017)

Please help!!

You can use code behind file something like lb1.SelectionMode = System.Web.UI.WebControls.ListSelectionMode.Multiple; or better where you declare it in your aspx <asp:ListBox ID="lb1" SelectionMode ="Multiple" runat ="server" /> and in winforms its listBox1.SelectionMode = SelectionMode.MultiExtended;
I don't use Web Forms but I replied to one of your other threads related to this and, when I read the documentation for the Web Forms ListBox class - which is exactly what you should have done - I found that SelectionMode property and also the GetSelectedIndices method. The documentation for that method says this:
Use the GetSelectedIndices method to identify or access the selected items in the ListBox control. Each element in the returned array represents the index for a selected list item. You can use the index values to access the items in the Items collection.
and that's exactly what I did in the code I posted in that thread. That's with no prior experience with that particular ListBox control. This is why I always tell people that they should read the documentation first. You won;t always find what you need or understand what you find but you often will, so check first. I've been doing just that since my own beginner days, enabling me to answer questions on subjects with which I had no prior experience even back then.
Top Bottom