Click here to Skip to main content
15,867,838 members
Articles / Web Development / ASP.NET
Article

Multi select Dropdown list in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.67/5 (33 votes)
19 Mar 20072 min read 620.3K   25.6K   99   43
Multiselect dropdown list control allows the user to select multiple items from the list and displays the selected items in comma separated format.

Introduction

The Dropdown list in ASP.NET is used to select a single item from the list. The Listbox control is used to select multiple items, but it takes up more space on the page. An ASP.NET page which contain many of these controls may make it difficult to find the proper space and alignment for each control.

To overcome this, I am introducing a Multi Select Dropdown list box control. It is a user control and can be used directly on pages very easily. It allows the user to select multiple items from the list and the selected items will be displayed in comma separated format in the text area, and it can also persist the selected items.

How to use this Control

The user control MultiSelectDropDown can be placed on any Web page.

Drag and drop the MultiSelectDropDown control on the web page where you want to use the multi select feature.

Populate the dropdown with appropriate values. In my sample it has been populated as follows:

C#
private void Page_Load(object sender, System.EventArgs e)
{
   if (!Page.IsPostBack )
   {
    MultiSelectDropDown1.Clear();
    MultiSelectDropDown1.List.Items.Add(new System.Web.UI.WebControls.ListItem("Apple","1")) ;
    MultiSelectDropDown1.List.Items.Add(new System.Web.UI.WebControls.ListItem("Grapes","2")) ;
    MultiSelectDropDown1.List.Items.Add(new System.Web.UI.WebControls.ListItem("Orange","3")) ;
    MultiSelectDropDown1.List.Items.Add(new System.Web.UI.WebControls.ListItem("Strawberry","4")) ;
    MultiSelectDropDown1.List.Items.Add(new System.Web.UI.WebControls.ListItem("Water Melon","5")) ;
   }
}

System.Collections.ArrayList selTexts = MultiSelectDropDown1.SelectedTexts;

SelectedTexts property returns an Arraylist of the selected items in the list, SelectedValues property

The following are the main public properties provided by this control:

SelectedTexts Arraylist of selected texts in the list
SelectedValues Arraylist of values corresponding to the selected items in the list
SelectedText Comma separated string of selected texts
SelectedItems Arraylist of values and texts corresponding to the selected items in the list
ListWidth Set/Get the width of the control
List List of items

Screenshots

The following screenshot shows how to select multiple items from the list:

Image 1

The width of the control can be changed by the ListWidth property of the control.

C#
MultiSelectDropDown1.ListWidth = Convert.ToDouble(txtWidth.Text);

The following image shows that the control width has changed to 150 from 250 when you enter 150 in the Dropdown width field and click on Set DD width button.

Image 2

The dropdown also shows the tooltip of the selected texts in comma separated format. It helps the user to find out what the selected items are, without clicking on the dropdown if the width of the dropdown is not large enough to display all the selected texts.

The following image shows the tooltip of the text:

Image 3

The selected items in the list will be added to the Selected Fruits Listbox when you click on Display selected fruits button.

The code is given below:

C#
ListBox1.Items.Clear();
System.Collections.ArrayList selItems = MultiSelectDropDown1.SelectedTexts;
ListBox1.DataSource = selItems;
ListBox1.DataBind();

The following code shows how to set the selected items of the control:

C#
ArrayList selectedItems = new ArrayList();
// Select the items from the list
foreach (System.Web.UI.WebControls.ListItem selItem in ListBox1.Items )
{
   System.Web.UI.WebControls.ListItem li = ListBox1.Items.FindByText(selItem.Text ) ;
   selectedItems.Add(li);
}
MultiSelectDropDown1.SelectedItems =selectedItems;

It sets the SelectedItems property of the control with the ArrayList of items.

Conclusion

This is a very useful and simple control and can be used on any web page irrespective of the .NET version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionupdate please Pin
manoj dhyani20-Jun-17 20:13
manoj dhyani20-Jun-17 20:13 
Questionvery important Pin
manoj dhyani29-Mar-17 4:35
manoj dhyani29-Mar-17 4:35 
QuestionHow would you bind this to a SQL:DataSource Pin
Member 974279311-Jan-16 4:45
Member 974279311-Jan-16 4:45 
QuestionNot Working with IE 9 Pin
bgates19704-Feb-15 5:51
bgates19704-Feb-15 5:51 
Question[My vote of 1] Multiselect dropdown Pin
ritika luthra7-Sep-14 22:47
ritika luthra7-Sep-14 22:47 
QuestionControl use doubts Pin
Member 102186741-Sep-14 6:49
Member 102186741-Sep-14 6:49 
QuestionMultiSelect Dropdown List(Custom Control) Pin
Member 1065311111-Mar-14 2:34
Member 1065311111-Mar-14 2:34 
QuestionMulti select Dropdown list in ASP.NET Pin
Sabyasachi Misra6-Nov-13 0:58
professionalSabyasachi Misra6-Nov-13 0:58 
Questionbrowser compatebale Pin
Member 1002984726-Sep-13 19:28
Member 1002984726-Sep-13 19:28 
AnswerRe: browser compatebale Pin
Sabyasachi Misra6-Nov-13 2:11
professionalSabyasachi Misra6-Nov-13 2:11 
yesThumbs Up | :thumbsup:
QuestionHow to Highlight items in Multi dropdown on Mouseover Pin
chanikya0327-Jun-13 21:15
chanikya0327-Jun-13 21:15 
QuestionCreate Dropdown with checkbox in mvc4 Pin
sonihemant23-Apr-13 20:30
sonihemant23-Apr-13 20:30 
QuestionCan we implement the same using JQuery? Pin
sweatha.murthy5-Jun-12 20:52
sweatha.murthy5-Jun-12 20:52 
AnswerRe: Can we implement the same using JQuery? Pin
vinodkumarnie29-Mar-13 19:36
vinodkumarnie29-Mar-13 19:36 
QuestionMultiSelectDropDown Pin
Shivani Sinha8-May-12 21:24
professionalShivani Sinha8-May-12 21:24 
QuestionNOT WORKING IN CONTENT PLACE HOLDER PAGE Pin
naveen511015-Sep-11 10:33
naveen511015-Sep-11 10:33 
AnswerRe: NOT WORKING IN CONTENT PLACE HOLDER PAGE Pin
NeerajGuru23-Mar-12 10:27
NeerajGuru23-Mar-12 10:27 
Questionhow to get the content or title value of respective page source url [modified] using asp.net Pin
manivannnanpon16-Aug-10 0:01
manivannnanpon16-Aug-10 0:01 
GeneralMulti-Select Drop Down List in ASP.NET Pin
Richard Xiong11-Aug-10 14:07
Richard Xiong11-Aug-10 14:07 
GeneralMy vote of 4 Pin
prashant0556.s.more26-Jul-10 0:42
prashant0556.s.more26-Jul-10 0:42 
GeneralDear Nair Madam Pin
tharuraju21-Jul-10 7:15
tharuraju21-Jul-10 7:15 
GeneralIm Not able to select items. Pin
Member 422664018-Apr-10 20:16
Member 422664018-Apr-10 20:16 
QuestionCan you Guide me, I am new Pin
Trupti Mehta6-Aug-09 0:29
Trupti Mehta6-Aug-09 0:29 
AnswerRe: Can you Guide me, I am new Pin
vinodkumarnie29-Mar-13 19:39
vinodkumarnie29-Mar-13 19:39 
QuestionCould you please guide me to bind a dataset to this user control? Pin
Harikaran S20-Jul-09 3:27
Harikaran S20-Jul-09 3:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.