Click here to Skip to main content
11,702,396 members (72,450 online)
Click here to Skip to main content

Multiselect Dropdown for Web Applications

, 17 Dec 2008 CPOL 186K 5.5K 168
Rate this:
Please Sign up or sign in to vote.
How to select multiple values from 'a dropdown look & feel' in a Web application

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.

<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:

.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).

// 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.

<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)

Share

About the Author

Sandeep Mewara
Software Developer (Senior)
India India
Current location: Bangalore, India.

You may also be interested in...

Comments and Discussions

 
AnswerRe: listbox value instead of text Pin
vsnewcomer30-Apr-12 11:16
membervsnewcomer30-Apr-12 11:16 
Questionmy vote for 10 Pin
vsnewcomer30-Apr-12 4:46
membervsnewcomer30-Apr-12 4:46 
GeneralMy vote of 5 Pin
manoj kumar choubey3-Feb-12 19:39
membermanoj kumar choubey3-Feb-12 19:39 
Answerdidn't display Pin
Member 798862127-Jul-11 0:03
memberMember 798862127-Jul-11 0:03 
Generali like it. thanks. Pin
xfblue2-May-11 22:57
memberxfblue2-May-11 22:57 
QuestionHow to get the checkboxlist selected id? Pin
alyy040622-Mar-11 21:25
memberalyy040622-Mar-11 21:25 
AnswerRe: How to get the checkboxlist selected id? Pin
Sandeep Mewara23-Mar-11 2:31
mvpSandeep Mewara23-Mar-11 2:31 
GeneralRe: How to get the checkboxlist selected id? Pin
E$w@r23-Mar-11 3:03
memberE$w@r23-Mar-11 3:03 
GeneralRe: How to get the checkboxlist selected id? Pin
alyy040628-Mar-11 15:23
memberalyy040628-Mar-11 15:23 
GeneralMy vote of 5 Pin
E$w@r25-Feb-11 1:48
memberE$w@r25-Feb-11 1:48 
GeneralRe: My vote of 5 Pin
Sandeep Mewara23-Mar-11 2:20
mvpSandeep Mewara23-Mar-11 2:20 
Generalthe project is not working in Firefox Pin
serhatergun24-Nov-10 21:17
memberserhatergun24-Nov-10 21:17 
GeneralRe: the project is not working in Firefox Pin
Sandeep Mewara24-Nov-10 22:02
mentorSandeep Mewara24-Nov-10 22:02 
GeneralRe: the project is not working in Firefox Pin
serhatergun24-Nov-10 22:29
memberserhatergun24-Nov-10 22:29 
GeneralRe: the project is not working in Firefox Pin
niceraj28-Dec-10 21:06
memberniceraj28-Dec-10 21:06 
GeneralRe: the project is not working in Firefox Pin
cxtay28-Feb-11 17:23
membercxtay28-Feb-11 17:23 
GeneralRe: the project is not working in Firefox Pin
Rajiv nayan11-Oct-12 0:13
memberRajiv nayan11-Oct-12 0:13 
GeneralRe: the project is not working in Firefox Pin
Rajiv nayan11-Oct-12 0:16
memberRajiv nayan11-Oct-12 0:16 
<script type="text/javascript">                
            var timoutID;
//           function ShowMList()
//           {
//                 var divRef = document.getElementById("divCheckBoxList");
//                 divRef.style.display = "block";
//                 var divRefC = document.getElementById("divCheckBoxListClose");
//                 divRefC.style.display = "block";
//           }
//          
//           function HideMList()
//           {
//                 document.getElementById("divCheckBoxList").style.display = "none";  
//                 document.getElementById("divCheckBoxListClose").style.display = "none";  
//           }
function ShowMList()
{
   var divRef = document.getElementById("ctl00_ContentPlaceHolder1_divCheckBoxList"); //notice the ct100_ prefix!
   divRef.style.display = "block";
   var divRefC = document.getElementById("ctl00_ContentPlaceHolder1_divCheckBoxListClose");
   divRefC.style.display = "block";
}
          
function HideMList()
{
   document.getElementById("ctl00_ContentPlaceHolder1_divCheckBoxList").style.display = "none";  
   document.getElementById("ctl00_ContentPlaceHolder1_divCheckBoxListClose").style.display = "none";  
}
            function FindSelectedItems(sender,textBoxID)
            {        
                  var cblstTable = document.getElementById(sender.id);
                  var checkBoxPrefix = sender.id + "_";
                  var noOfOptions = cblstTable.rows.length;
                  var selectedText = "";
                  //alert(checkBoxPrefix);
                  for(i=0; i < noOfOptions ; ++i)
                  {
                        if(document.getElementById(checkBoxPrefix+i).checked)
                        {
                              if(selectedText == "")
                              {
                                    if(document.getElementById(checkBoxPrefix+i).parentNode.innerText != null)
                                    {
                                          selectedText = document.getElementById(checkBoxPrefix+i).parentNode.innerText;
                                    }
                                    else
                                    {
                                          selectedText = document.getElementById(checkBoxPrefix+i).parentNode.textContent;
                                    }
                              }  
                              else
                              if(document.getElementById(checkBoxPrefix+i).parentNode.innerText != null)
                                    {
                                          selectedText = selectedText + "," + document.getElementById(checkBoxPrefix+i).parentNode.innerText;
                                    }
                                    else
                                    {
                                          selectedText = selectedText + "," + document.getElementById(checkBoxPrefix+i).parentNode.textContent;
                                    }
                        }
                  }
                  document.getElementById(textBoxID.id).value = selectedText;  
            }
      </script>
GeneralSelect All Pin
harishkakani7-Jan-10 4:29
memberharishkakani7-Jan-10 4:29 
AnswerRe: Select All Pin
Sandeep Mewara7-Jan-10 22:18
memberSandeep Mewara7-Jan-10 22:18 
GeneralIts not working in the page, which have Master page Pin
Vishnu Narayan Mishra19-Oct-09 1:14
memberVishnu Narayan Mishra19-Oct-09 1:14 
AnswerRe: Its not working in the page, which have Master page Pin
Sandeep Mewara7-Jan-10 22:19
memberSandeep Mewara7-Jan-10 22:19 
GeneralRe: Its not working in the page, which have Master page Pin
Rajiv nayan11-Oct-12 0:18
memberRajiv nayan11-Oct-12 0:18 
GeneralRe: Its not working in the page, which have Master page Pin
Rajiv nayan11-Oct-12 0:19
memberRajiv nayan11-Oct-12 0:19 
GeneralRe: Its not working in the page, which have Master page Pin
thund3rstruck4-May-10 3:59
memberthund3rstruck4-May-10 3:59 
GeneralRe: Its not working in the page, which have Master page Pin
er.puneet25-Aug-10 21:44
memberer.puneet25-Aug-10 21:44 
GeneralRe: Its not working in the page, which have Master page Pin
Rajiv nayan11-Oct-12 0:15
memberRajiv nayan11-Oct-12 0:15 
GeneralThanks Pin
FBR28-Jul-09 22:21
memberFBR28-Jul-09 22:21 
AnswerRe: Thanks Pin
Sandeep Mewara7-Jan-10 22:20
memberSandeep Mewara7-Jan-10 22:20 
GeneralHello India Boy Pin
defineconst23-Jun-09 17:25
memberdefineconst23-Jun-09 17:25 
AnswerRe: Hello India Boy Pin
Sandeep Mewara7-Jan-10 22:20
memberSandeep Mewara7-Jan-10 22:20 
GeneralMultiple selection DDL with Search and Select all option. Pin
deepak_rai23-Mar-09 2:40
memberdeepak_rai23-Mar-09 2:40 
AnswerRe: Multiple selection DDL with Search and Select all option. Pin
Sandeep Mewara6-Apr-09 0:48
memberSandeep Mewara6-Apr-09 0:48 
GeneralCode associated to a selection Pin
Felipe Acosta8-Dec-08 16:03
memberFelipe Acosta8-Dec-08 16:03 
AnswerRe: Code associated to a selection Pin
Sandeep Mewara9-Dec-08 6:37
memberSandeep Mewara9-Dec-08 6:37 
GeneralExcellent article Pin
Swethapriya20-Nov-08 19:08
memberSwethapriya20-Nov-08 19:08 
AnswerRe: Excellent article Pin
Sandeep Mewara26-Nov-08 18:27
memberSandeep Mewara26-Nov-08 18:27 
GeneralNice! Pin
JimWhite_Aus18-Nov-08 1:01
memberJimWhite_Aus18-Nov-08 1:01 
AnswerRe: Nice! Pin
JonnyLA26-Nov-08 8:06
memberJonnyLA26-Nov-08 8:06 
AnswerRe: Nice! Pin
Sandeep Mewara26-Nov-08 18:28
memberSandeep Mewara26-Nov-08 18:28 
GeneralGreat Article Pin
AreJay4-Nov-08 4:27
memberAreJay4-Nov-08 4:27 
GeneralRe: Great Article Pin
Sandeep Mewara9-Nov-08 9:24
memberSandeep Mewara9-Nov-08 9:24 
GeneralThanks Nice [modified] Pin
EvilInside20-Oct-08 19:09
memberEvilInside20-Oct-08 19:09 
AnswerRe: Thanks Nice Pin
Sandeep Mewara20-Oct-08 20:20
memberSandeep Mewara20-Oct-08 20:20 
AnswerRe: Thanks Nice Pin
Sandeep Mewara20-Oct-08 21:33
memberSandeep Mewara20-Oct-08 21:33 
GeneralA couple of things!! Pin
m_irfan14-Oct-08 11:03
memberm_irfan14-Oct-08 11:03 
AnswerRe: A couple of things!! Pin
Sandeep Mewara15-Oct-08 2:49
memberSandeep Mewara15-Oct-08 2:49 
GeneralBUG: No scroll bar showed up Pin
BYQL1-Oct-08 13:34
memberBYQL1-Oct-08 13:34 
AnswerRe: BUG: No scroll bar showed up Pin
Sandeep Mewara3-Oct-08 4:19
memberSandeep Mewara3-Oct-08 4:19 
AnswerRe: BUG: No scroll bar showed up Pin
Sandeep Mewara20-Oct-08 20:33
memberSandeep Mewara20-Oct-08 20:33 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 17 Dec 2008
Article Copyright 2008 by Sandeep Mewara
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid