Click here to Skip to main content
5,786,882 members and growing! (24,946 online)
Email Password   helpLost your password?
Desktop Development » Combo & List Boxes » General     Intermediate License: The Code Project Open License (CPOL)

Multiselect DropDown ListBox control for Web Applications

By Saleena, Thangavel Murugesan

Enables the user to select multiple options in a drop down.
C#.NET 1.0, .NET 1.1, .NET 2.0, .NET, Dev

Posted: 10 Nov 2008
Updated: 3 Dec 2008
Views: 7,863
Bookmarked: 23 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
8 votes for this Article.
Popularity: 3.16 Rating: 3.50 out of 5
2 votes, 25.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 12.5%
4
5 votes, 62.5%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
MultiSelect-DropDown

Introduction

The developer community has come across many scenarios where the customer would like to make multiple choices and the developer wishes to make intelligent usage of screen space. The objective of "multi select drop down list" (MSDD) control is to allow the user select multiple options as in a check box list that behaves like a dropdown list. The multi select dropdown list (MSDD) will consume the space as of a dropdown list and deliver the functionality of a Check Box list.

Motivation Factor

By default, the .Net web programmers go by using the ListBox control with multi select option or the checkbox list. No doubt many of us would have felt the need to integrate the dropdown list and the checklist box. We have done just that in this article.

Features

This user control is built using C# as server side and JavaScript for client side scripting. The following lists the unique features: 1. Ability to select multiple items.
2. Ability to attach java script functions to check/uncheck events
3. Higher z-index compared to the other web controls such as List box and dropdown which have relatively higher Z-index than the normal web controls. This prevents the List box/dropdown list in a page overriding the multi select dropdown panel.
4. Highlight property - highlights the item when the mouse hovers over the item. The highlight color is fixed.
5. Filter property - moves to the item with the alphabet keyed , only when the scroll bar appears.

About the control

Important properties:

1. Client scripts can be added using attributes to the control
2. The selected items can be read as either comma separated values or as array list. The seleceted items will also be displayed by the tooltip.
3. The text "All" and "Please select" will be displayed when all the options are selected or none selected respectively.
5. Hooking the event handlers.

Usage Flow

Initialising the Control

The control has to be initialized on page load, if the properties size, colors etc are set at runtime rather than at the design time.
  private void InitialiseDropDownControls()
        {
            // To set the background colors
           // multiselect_foodItems.TextBoxBackColor = System.Drawing.Color.FromName("#FFFFE6");
           // multiselect_foodItems.DropDownBackColor = System.Drawing.Color.Aqua;
            // Set the height and width of dropdown controls.The defualt size is set in pixels.
            multiselect_foodItems.Width = 160;
            multiselect_foodItems.Height = 15;

            multiselect_Juices.Width = 160;
            multiselect_Juices.Height= 15;
 //Any attributes to be added to the checkboxlist.These events will be performed ahead of the default events that come with the control
            multiselect_foodItems.AddAttribute("onclick", "alert('test');");
        }

Setting the Datasource 

The multiselect drop-down supports the dataset and the list items as the data source. It is not yet tested with other data sources such as object, xml etc.

private void LoadData()
        {
            //Biniding the Dataset as the datasource
            multiselect_foodItems.DataSource = GetFoodItems().Tables[0];
            multiselect_foodItems.DataTextField = "FoodItems";
            multiselect_foodItems.DataBind();
            // Using the list item to add items
            multiselect_Juices.Items.Add("Apple");
            multiselect_Juices.Items.Add(new ListItem("Orange"));
            ListItem item = new ListItem("Grapes");
            multiselect_Juices.Items.Add(item);
        }

Attaching Scripts

The javascript functions can be attached to the check/uncheck event as follows:

 multiselect_foodItems.AddAttribute("onclick", "alert('test');"); 

Any events that are added to the default events (those that come along with the control such as the "onclick" event) will be performed after the default event. This will help the user to perform actions apart from the defaults. For eg: Enable another control based on the value selected.

Collapse dropdown

The dropdown opens up when user clicks anywhere on the control and is hidden when the user clicks anywhere outside the control.

Events

The server side event handler for checkbox selected item change has been provided. The event has to be hooked on the containing class as follows:

	multiselect_Juices.OnMSDDSelectedIndexChanged += new Controls.MultiselectDropDownListBox.OnList_SelectedIndexChangedEventHandler(multiselect_Juices_OnMSDDSelectedIndexChanged);
multiselect_Juices_OnMSDDSelectedIndexChanged is the method that will be invoked on the event.

Changing the Highlight color:

If the user is interested in changing the highlight color, he can do so in the style sheet class itemSelected"code-string">"code-string">.

Challenges Faced

Why CheckBoxList rather than adding the html checkbox at runtime?

Initiallly, we thought of constructing a dynamic checkbox within a div on the server side and display/hide the div on click events. However, we realised that we will have problems in retaining state, for server side operations and will need to loop through the items when bound to the dataobjects.Hence, the checkbox list was chosen which will make things easier.

Attributes of ListItem

On .NET Frameworks 1.0 and 1.1, the attributes added to the listitem are not rendered. This is a bug with all the controls using the listitem. This had to be circumvented while the page is rendered. On .Netframework 2.0, this has been rectified and the attributes of the listitem are rendered. However, when the dataset or any other datasource is bound to the checkbox list, item highlight "onmouseover" and to check/uncheck items when clicked anywhere on the item (not specifically on the label/checkbox) will need to be handled while the control is rendered.

Positioning of the CheckBoxList

The positioning of the checkbox list to be in alignment with the text box has been calculated based on the offsetLeft of all its parents, but cannot be guranteed for accuracy in all cases. For proper positioning, if the control is placed inside html tables, remember to set the cellpadding and cellspacing attributes. When these attributes are set to values other than zero and you feel that the checkbox list does not align with the text box, try modifying the curleft variable set in the Left function in MultiselectListBox.js file. Please note that this is valid only when the "RepeatLayout" property of the checkbox list has been set to "Table" (default value of the control).

if(obj.parentElement.tagName.toUpperCase() == "TABLE")
				    {
				        curleft -= Number(obj.parentElement.getAttribute('cellspacing'));
				        curleft -= Number(obj.parentElement.getAttribute('cellpadding'));

			        }

Higher Z-index for the Checkboxlist Control

When the div is positioned dynamically, the checkboxlist inside the div is overridden by controls such as the listbox which are actually placed beneath the div and the containing checkboxlist. To overcome this issue, an iframe was positioned as the div, so that the div gets a higher z-index value. The iframe overrides the listbox control and hence the div and the checkboxlist are shown. Here the src of the iframe is not set.

Enhancements

A MultiselectEventArgs derived from EventArgs can be used. This can hold the list of selected items and its corresponding indexes.
The code has been tested only on IE. Needs changes to make it compatible with other browsers .

History

Version - v1.1. - fixed the scrolling issue

License

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

About the Authors

Saleena



Location: India India

Thangavel Murugesan



Occupation: Software Developer
Company: iNautix Technologies India Pvt Ltd
Location: India India

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
QuestionDatasetmemberDanielHH8:20 27 Nov '08  
AnswerRe: DatasetmemberSaleena2:29 29 Nov '08  
QuestionRe: DatasetmemberDanielHH6:00 29 Nov '08  
AnswerRe: DatasetmemberSaleena21:40 29 Nov '08  
GeneralRe: DatasetmemberDanielHH11:00 30 Nov '08  
GeneralRe: DatasetmemberSaleena21:12 30 Nov '08  
QuestionRe: DatasetmemberDanielHH8:12 2 Dec '08  
AnswerRe: DatasetmemberSaleena7:57 3 Dec '08  
QuestionRe: DatasetmemberDanielHH11:12 3 Dec '08  
AnswerRe: DatasetmemberSaleena19:49 3 Dec '08  
QuestionRe: DatasetmemberDanielHH12:07 4 Dec '08  
AnswerRe: DatasetmemberSaleena7:22 5 Dec '08  
QuestionRe: DatasetmemberDanielHH4:07 6 Dec '08  
AnswerRe: DatasetmemberSaleena7:35 6 Dec '08  
GeneralMy vote of 1memberSandeep Mewara1:52 26 Nov '08  
GeneralRe: My vote of 1memberSaleena0:37 27 Nov '08  
RantRe: My vote of 1 - for your message!membertimh_em23:21 3 Dec '08  
AnswerRe: My vote of 1 - for your message!memberSandeep Mewara7:25 7 Dec '08  
GeneralPlease fix your linksmemberfwsouthern10:21 11 Nov '08  
GeneralRe: Please fix your linksmemberSaleena19:45 11 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Dec 2008
Editor: Sean Ewington
Copyright 2008 by Saleena, Thangavel Murugesan
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project