Click here to Skip to main content
15,860,859 members
Articles / Web Development / XHTML
Article

Multiselect Dropdown for Web Applications

Rate me:
Please Sign up or sign in to vote.
4.83/5 (82 votes)
17 Dec 2008CPOL2 min read 292.8K   6.7K   168   80
How to select multiple values from 'a dropdown look & feel' in a Web application
Image 1

Introduction

Whenever there is a requirement of selecting multiple values in a Web application, where space holds an importance and one needs a client-interactive User Interface – a control that has an auto-collapse feature and looks like multiple select dropdown is a good fit.

This is a control that allows a user to select multiple values through a dropdown. It clubs input element, an image and a checkboxlist to give a feel of multi-selectable dropdown. It has an auto-collapse feature apart from explicit close option giving it a total feel of a normal dropdown. The control can be tweaked by developers as per their own needs very easily. The code sample of the same is attached with the article.

Background

We needed to provide an option of multi-select in our project lately. For multiple select, ASP.NET provides CheckBoxList which takes quite a lot of space and does not always fit in the UI requirements. In our UI and as per the customer's request, we were asked to use a dropdown like feature that can select multiple values (just like one of the controls in .NET Windows). I searched on The Code Project and Googled it but could hardly find any control that gave a dropdown look and allowed multiple select. Some were there but they weren't free! So, I went ahead and made my own. And since I was making it myself, I added all the features that I thought would be user friendly.

Using the Code

The first thing was to place the controls to give the look and feel of a dropdown. This was achieved in HTML using proper CSS for the controls.

HTML
<div id="divCustomCheckBoxList" runat="server" onmouseover="clearTimeout(timoutID);"
         onmouseout="timoutID = setTimeout('HideMList()', 750);">
    <table>
        <tr>
            <td align="right" class="DropDownLook">
               <input id="txtSelectedMLValues" type="text" readonly="readonly"
                 onclick="ShowMList()" style="width:229px;" runat="server" />
            </td>
            <td align="left" class="DropDownLook">
               <img id="imgShowHide" runat="server" src="drop.gif"
                          onclick="ShowMList()" align="left" />
            </td>
        </tr>
        <tr>
            <td colspan="2" class="DropDownLook">
               <div>
                   <div runat="server" id="divCheckBoxListClose" class="DivClose">
                         <label runat="server" onclick="HideMList();"
                                   class="LabelClose" id="lblClose"> x</label>
                     </div>
                   <div runat="server" id="divCheckBoxList" class="DivCheckBoxList">
                         <asp:CheckBoxList ID="lstMultipleValues" runat="server"
                          Width="250px" CssClass="CheckBoxList"></asp:CheckBoxList>
                     </div>
                 </div>
            </td>
        </tr>
    </table>
</div> 

CSS has a very important part out here to give it a dropdown look:

CSS
.DivClose
{
         display:none;
         position:absolute;
         width:250px;
         height:220px;
         border-style:solid;
         border-color:Gray;
         border-width:1px;
         background-color:#99A479;
}

.LabelClose
{
         vertical-align:text-top;
         position:absolute;
         bottom:0px;
         font-family:Verdana;
}

.DivCheckBoxList
{
         display:none;
         background-color:White;
         width:250px;
         position:absolute;
         height:200px;
         overflow-y:auto;
         overflow-x:hidden;
         border-style:solid;
         border-color:Gray;
         border-width:1px;
}

.CheckBoxList
{
         position:relative;
         width:250px;
         height:10px;
         overflow:scroll;
         font-size:small;
}

While the CheckBoxList control was populated, a JavaScript handler was attached to it such that selection/de-selection of any option reflects in the input element (in selected values placed in a dropdown).

C#
// Append an event to the checkboxes in the list
lstMultipleValues.Attributes.Add("onclick", "FindSelectedItems
         (this," + txtSelectedMLValues.ClientID + ");");

Using JavaScript, we keep track of what is selected in the list. Further, the open and close of the div-elements are also handled through JavaScipts using onMouseOver, onMouseOut events.

JavaScript
<script type="text/javascript">
   var timoutID;

   //This function shows the checkboxlist
   function ShowMList()
   {
       var divRef = document.getElementById("divCheckBoxList");
       divRef.style.display = "block";
       var divRefC = document.getElementById("divCheckBoxListClose");
       divRefC.style.display = "block";
   }

   //This function hides the checkboxlist
   function HideMList()
   {
       document.getElementById("divCheckBoxList").style.display = "none";
       document.getElementById("divCheckBoxListClose").style.display = "none";
   }

   //This function finds the checkboxes selected in the list and using them,
   //it shows the selected items text in the textbox (comma separated)
   function FindSelectedItems(sender,textBoxID)
   {
       var cblstTable = document.getElementById(sender.id);
       var checkBoxPrefix = sender.id + "_";
       var noOfOptions = cblstTable.rows.length;
       var selectedText = "";
       for(i=0; i < noOfOptions ; ++i)
       {
          if(document.getElementById(checkBoxPrefix+i).checked)
          {
             if(selectedText == "")
                selectedText = document.getElementById
                                   (checkBoxPrefix+i).parentNode.innerText;
             else
                selectedText = selectedText + "," +
                 document.getElementById(checkBoxPrefix+i).parentNode.innerText;
          }
       }
       document.getElementById(textBoxID.id).value = selectedText;
    }
</script>

The client side events of controls are used in a way such that as long as we have a mouse over the control (textbox or dropdown or checkboxlist or scroll) the list is open. Once the mouse leaves the control, after a certain time (configurable), it gets collapsed automatically. To add more flexibility for the user, a close option is also provided at the bottom of the control. Developers can tweak the control as per their requirements.

Points of Interest

The work was appreciated by the team members and the client finds it to be a good, clean way to provide users with a multi-select option! Last but not the least, it is free!

History

  • 14th December, 2008: Wordings changed
  • 25th September, 2008: Initial post

License

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


Written By
Architect Intuit India
India India


A software professional for more than 17+ years building solutions for Web and Desktop applications.

Currently working at Intuit India.

Website: Learn By Insight
Github: Sandeep Mewara
LinkedIn: Sandeep Mewara

Strongly believe in learning and sharing knowledge.



Comments and Discussions

 
QuestionHow to set Selected value Pin
ritesh_kk20002-Nov-20 13:53
ritesh_kk20002-Nov-20 13:53 
QuestionMultiSelect DropdownList Using jQuery in Asp.Net MVC and C#.Net Pin
Member 123685433-Mar-16 18:41
Member 123685433-Mar-16 18:41 
QuestionMicrosoft JS runtime error 'txtSelectedMLValues' is undefined. Pin
Member 120344545-Oct-15 6:26
Member 120344545-Oct-15 6:26 
QuestionConversion to vb.net code Pin
Member 1144944719-Feb-15 7:48
Member 1144944719-Feb-15 7:48 
Questionused the code but getting the errors Pin
Member 1139425522-Jan-15 1:20
Member 1139425522-Jan-15 1:20 
QuestionMicrosoft JScript runtime error: 'txtSelectedMLValues' is undefined Pin
Mubarak H Rahman31-Dec-14 4:08
Mubarak H Rahman31-Dec-14 4:08 
AnswerRe: Microsoft JScript runtime error: 'txtSelectedMLValues' is undefined Pin
Mubarak H Rahman31-Dec-14 4:48
Mubarak H Rahman31-Dec-14 4:48 
GeneralRe: Microsoft JScript runtime error: 'txtSelectedMLValues' is undefined Pin
Member 120344545-Oct-15 6:27
Member 120344545-Oct-15 6:27 
SuggestionWonderful Project -- Worked perfectly once I updated to VS2012 (.NET 4.5 Framework) Pin
danlkb215-Apr-14 5:47
danlkb215-Apr-14 5:47 
GeneralRe: Wonderful Project -- Worked perfectly once I updated to VS2012 (.NET 4.5 Framework) Pin
Sandeep Mewara15-Apr-14 18:38
mveSandeep Mewara15-Apr-14 18:38 
QuestionGreat one Pin
nietzky19-Jun-13 1:48
nietzky19-Jun-13 1:48 
GeneralMy vote of 5 Pin
Sarvesvara (BVKS) Dasa24-Dec-12 18:14
Sarvesvara (BVKS) Dasa24-Dec-12 18:14 
GeneralMy vote of 5 Pin
Savalia Manoj M18-Nov-12 16:48
Savalia Manoj M18-Nov-12 16:48 
QuestionWhen Use MASTER page then change some code like........... Pin
Rajiv nayan11-Oct-12 0:18
Rajiv nayan11-Oct-12 0:18 
Questionlistbox value instead of text Pin
vsnewcomer30-Apr-12 10:51
vsnewcomer30-Apr-12 10:51 
AnswerRe: listbox value instead of text Pin
vsnewcomer30-Apr-12 11:16
vsnewcomer30-Apr-12 11:16 
Questionmy vote for 10 Pin
vsnewcomer30-Apr-12 4:46
vsnewcomer30-Apr-12 4:46 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey3-Feb-12 19:39
professionalManoj Kumar Choubey3-Feb-12 19:39 
Answerdidn't display Pin
Member 798862127-Jul-11 0:03
Member 798862127-Jul-11 0:03 
Generali like it. thanks. Pin
xfblue2-May-11 22:57
xfblue2-May-11 22:57 
QuestionHow to get the checkboxlist selected id? Pin
alyy040622-Mar-11 21:25
alyy040622-Mar-11 21:25 
AnswerRe: How to get the checkboxlist selected id? Pin
Sandeep Mewara23-Mar-11 2:31
mveSandeep Mewara23-Mar-11 2:31 
GeneralRe: How to get the checkboxlist selected id? Pin
TweakBird23-Mar-11 3:03
TweakBird23-Mar-11 3:03 
GeneralRe: How to get the checkboxlist selected id? Pin
alyy040628-Mar-11 15:23
alyy040628-Mar-11 15:23 
GeneralMy vote of 5 Pin
TweakBird25-Feb-11 1:48
TweakBird25-Feb-11 1:48 

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.