35

Select multiple value in DropDownList using ASP.NET and C#. I tried it to select single value from drop down but unable to find multiple selection.

Osama AbuSitta
  • 3,918
  • 4
  • 35
  • 51
Er Jay Thakkar
  • 401
  • 1
  • 4
  • 8

5 Answers5

42

In that case you should use ListBox control instead of dropdown and Set the SelectionMode property to Multiple

<asp:ListBox runat="server" SelectionMode="Multiple" >
  <asp:ListItem Text="test1"></asp:ListItem>
  <asp:ListItem Text="test2"></asp:ListItem>
  <asp:ListItem Text="test3"></asp:ListItem>
</asp:ListBox>
Sachin
  • 40,216
  • 7
  • 90
  • 102
  • 4
    can we get checkboxes to select multiple values – Er Jay Thakkar Sep 17 '13 at 10:28
  • 1
    @downvoter.. Please let me the suitable reason for down vote and if there is better solution than this the please share that as well. – Sachin Sep 30 '14 at 07:52
  • 1
    @Sachin How to make the listbox only shows the options on user click? The list box is showing all the options by default. can we avoid this – Beingnin Aug 28 '17 at 04:50
  • You can refer this Link [Add and display CheckBoxList (CheckBoxes) in DropDownList](https://www.aspsnippets.com/Articles/Add-and-display-CheckBoxList-CheckBoxes-in-DropDownList-in-ASPNet.aspx) for dropdowncheckboxlist using bootstrap and multiselect. – Ashi Feb 13 '20 at 09:19
26

Take a look at the ListBox control to allow multi-select.

<asp:ListBox runat="server" ID="lblMultiSelect" SelectionMode="multiple">
            <asp:ListItem Text="opt1" Value="opt1" />
            <asp:ListItem Text="opt2" Value="opt2" />
            <asp:ListItem Text="opt3" Value="opt3" />
</asp:ListBox> 

in the code behind

foreach(ListItem listItem in lblMultiSelect.Items)
    {
       if (listItem.Selected)
       {
          var val = listItem.Value;
          var txt = listItem.Text; 
       }
    }
Julien G
  • 415
  • 1
  • 5
  • 20
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
2

Dropdown list wont allows multiple item select in dropdown.

If you need , you can use listbox control..

ASP.NET List Box

Community
  • 1
  • 1
bgs
  • 3,061
  • 7
  • 40
  • 58
2

For multiple selection dropdown list,cannot accomplish it directly using dropdown..Can be done in similar ways..

Either you have to use checkbox list or listbox (ajax inclusive)

http://www.codeproject.com/Articles/55184/MultiSelect-Dropdown-in-ASP-NET

http://social.msdn.microsoft.com/Forums/vstudio/en-US/54374df7-5a54-42bc-83b8-ad5994cb634d/multi-select-dropdownlist

http://www.dotnetfunda.com/articles/article1591-multiselect-dropdownlist-in-aspnet-using-csharp-40-.aspx

Sasidharan
  • 3,676
  • 3
  • 19
  • 37
0

If you are using a ListBox control and LINQ you can try this:

Code Example:

control.Items.Cast< ListItem >().Where(x => x.Selected).ToList();

I hope this solves your issue!

AztecCodes
  • 1,130
  • 7
  • 23
  • if you need selectedvalues ... ddl.Items.Cast().Where(x => x.Selected).Select(x => Convert.ToInt32(x.Value)).ToList(); – Alejandro Tapia Jul 25 '23 at 01:36
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 27 '23 at 16:39