Click here to Skip to main content
11,494,174 members (70,843 online)
Click here to Skip to main content

A Multiple Selection DropDownList (a CheckBoxList Inside a DropDownList)

, 19 Mar 2010 CPOL 128K 14.3K 62
Rate this:
Please Sign up or sign in to vote.
Check and mark more than one option in a DropDownList

Introduction

This ascx user control, the MultiCheckCombo, allows the user to select more than one option inside a DropDownList. The different items that are being checked are being copied in the box area of the control.

The great thing of this control is that it doesn't close the check panel until you click outside the control. Example: you can check more than one item at once.

In ASP.NET C#, if you need a web control that allows the user to check more than one option in a DropDownList, you can use this user control freely.

Background

Well, first of all, this control was made in Microsoft Visual Studio 2008, with .NET 3.5 (2.0) and was written in C#. To use it, you need to have properly installed the AjaxControlToolkit in your project.

Using the Code

Again: You need to have installed the AjaxControlToolkit in your project.

This control is fairly easy to implement. Simply copy the 3 ascx files into your project.

Then replace the namespace in both ascx and ascx.cs files with [YourProjectName].MultiCheckCombo.

E.g.: Let's suppose your project's name is TestMultiCheck, replace in the ascx file the Control tag with the name of your namespace/project:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MultiCheckCombo.ascx.cs" 
    Inherits="TestMultiCheck.MultiCheckCombo" %>

And in your ascx.cs file, change the namespace to:

namespace TestMultiCheck

Well, then drag and drop the main file (MultiCheckCombo.ascx) to your Page Designer.

You may drag the control inside or outside an UpdatePanel, but you need a ScriptManager at the beginning of the body tag of your aspx page.

<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> 

When you drag the control, the following line should be added automatically to the beginning of your aspx page:

<%@ Register src="MultiCheckCombo.ascx" tagname="MultiCheckCombo" tagprefix="uc1" %>

And the code of the control looks like this (it also will be added automatically):

<uc1:MultiCheckCombo ID="MultiCheckCombo1" runat="server" />        

Then, in the code behind (C#), you have to fill the values of the control.

You can fill it with an ArrayList or with an OdbcDataReader.

A: Easy mode: Filling the MultiCheckCombo with an ArrayList:

ArrayList array = new ArrayList();
for (int i=0;i<100;i++) array.Add(i.ToString());
MultiCheckCombo1.AddItems(array);

B: Other option is to fill the MultiCheckCombo with an OdbcDataReader:

OdbcConnection con = /* get YOUR connection string*/
con.Open();
OdbcCommand cmd = new OdbcCommand();
cmd.Connection = con;
cmd.CommandText = "select text,id from ...........";
OdbcDataReader dr = cmd.ExecuteReader();
MultiCheckCombo1.ClearAll();
dr.Read(); 
MultiCheckCombo1.AddItems(dr, "text", "id");

And that's it. You can now test it.

Some important properties of this controls are:

MultiCheckCombo1.WidthCheckListBox = 200; //Set the Width of the CheckBoxList inside DDL.
MultiCheckCombo1.Width = 250; //Set the Width of the DropDownList
MultiCheckCombo1.Enabled = true;
MultiCheckCombo1.fontSizeCheckBoxList = FontUnit.Small; 
MultiCheckCombo1.fontSizeTextBox = FontUnit.XSmall;
MultiCheckCombo1.Text; //Get or Set the Text shown in the Combo

Some important methods of this control are:

MultiCheckCombo1.unselectAllItems(); //Uncheck all of the Items of the CheckBox
MultiCheckCombo1.ClearAll(); //Delete all the Items of the CheckBox;

Points of Interest

I hope you can make it work. Cheers! ENJOY IT!

By the way, my e-mail is leoivix@hotmail.com.

License

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

Share

About the Author

leoivix
Web Developer
Argentina Argentina
Systems Eng.

Comments and Discussions

 
AnswerRe: RESOLVED FOR C#: Multiple MultiCheckCombo controls on same page, textboxes getting wrong selections / Plus solution to undefined showing up in explorer Pin
Member 415786215-Sep-12 7:47
memberMember 415786215-Sep-12 7:47 
GeneralRe: RESOLVED FOR C#: Multiple MultiCheckCombo controls on same page, textboxes getting wrong selections / Plus solution to undefined showing up in explorer Pin
oozz_6-Dec-12 3:15
memberoozz_6-Dec-12 3:15 
AnswerRe: RESOLVED FOR C#: Multiple MultiCheckCombo controls on same page, textboxes getting wrong selections / Plus solution to undefined showing up in explorer Pin
cooldiv4u31-May-13 7:06
membercooldiv4u31-May-13 7:06 
QuestionTakes up multiple lines in datagrid Pin
Member 219182726-Oct-11 9:13
memberMember 219182726-Oct-11 9:13 
AnswerRe: Takes up multiple lines in datagrid Pin
s.hashmi20-May-12 23:15
members.hashmi20-May-12 23:15 
AnswerRe: Takes up multiple lines in datagrid Pin
cooldiv4u31-May-13 6:26
membercooldiv4u31-May-13 6:26 
QuestionUndefined value in text box Pin
faisalharoon3-Oct-11 19:38
memberfaisalharoon3-Oct-11 19:38 
AnswerRe: Undefined value in text box Pin
leoivix4-Oct-11 3:10
memberleoivix4-Oct-11 3:10 
Hi faisalharoon. This problem sometimes happens in some versions of Firefox. Its a javascript problem and I dont know how to solve it. If anyone can solve this, please post it.
By the way, I send you a modification for the Text property of MultiCheckCombo.ascx.cs. Replace it. It will still show you the word Undefined in the control, but the server will read it in the correct way.


///
/// Get or Set the Text shown in the Combo
///

public string Text
{
get
{
string s = "";
for (int i = 0; i < chkList.Items.Count; i++)
{
if (chkList.Items[i].Selected)
{
s += "," + chkList.Items[i].Text;
}
}
if (s.Length > 0) s = s.Remove(0, 1);
return s;
}
set { txtCombo.Text = value; }
}
GeneralRe: Undefined value in text box Pin
faisalharoon5-Oct-11 23:59
memberfaisalharoon5-Oct-11 23:59 
AnswerRe: Undefined value in text box Pin
bimboy_14311-Nov-11 9:49
memberbimboy_14311-Nov-11 9:49 
Questioninstantiate via code Pin
mikeng11121-Sep-11 8:37
membermikeng11121-Sep-11 8:37 
Questionget ID and Text from it Pin
yasserzaid28-Aug-11 16:40
memberyasserzaid28-Aug-11 16:40 
AnswerRe: get ID and Text from it [modified] Pin
leoivix30-Aug-11 3:24
memberleoivix30-Aug-11 3:24 
GeneralMy vote of 5 Pin
Member 798862127-Jul-11 0:35
memberMember 798862127-Jul-11 0:35 
Generalmultiple MultiCheckCombo in same page Pin
Marco Trova17-Aug-10 0:04
memberMarco Trova17-Aug-10 0:04 
GeneralRe: multiple MultiCheckCombo in same page Pin
boonjiann16-Sep-10 19:23
memberboonjiann16-Sep-10 19:23 
GeneralRe: multiple MultiCheckCombo in same page Pin
math2021-Jul-11 19:33
membermath2021-Jul-11 19:33 
BugRe: multiple MultiCheckCombo in same page Pin
bhfaisal9-Aug-11 22:55
memberbhfaisal9-Aug-11 22:55 
GeneralRe: multiple MultiCheckCombo in same page [modified] Pin
07_irfan1-Mar-12 5:28
member07_irfan1-Mar-12 5:28 
GeneralRe: multiple MultiCheckCombo in same page Pin
s.hashmi20-May-12 23:24
members.hashmi20-May-12 23:24 
QuestionEvents for this conrol Pin
Thushar Saseendran4-Aug-10 4:11
memberThushar Saseendran4-Aug-10 4:11 
AnswerRe: Events for this conrol Pin
s.hashmi20-May-12 23:21
members.hashmi20-May-12 23:21 
GeneralLoad Error Pin
Megha0922-Jul-10 2:33
memberMegha0922-Jul-10 2:33 
Generalsome douts Pin
ravanth18-Mar-10 19:30
memberravanth18-Mar-10 19:30 
GeneralRe: some douts Pin
leoivix19-Mar-10 4:17
memberleoivix19-Mar-10 4:17 

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
Web03 | 2.8.150520.1 | Last Updated 19 Mar 2010
Article Copyright 2010 by leoivix
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid