Click here to Skip to main content
15,881,861 members
Articles / Web Development / ASP.NET

MultiSelect Dropdown in ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.33/5 (9 votes)
28 Jan 2010CPOL1 min read 151.4K   7.5K   21   14
Easy way to do multi select values in ASP.NET.

Introduction

In this article, I will show you a different way of doing a multi-select in an ASP.nET page. I will keep this article short and sweet so you can just use the code in your applications.

Using the code

We are going to put our CheckBoxList ASP.NET control inside an HTML div object. I have also added code to support changing the background color of the selected row.

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" 
  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">
.scroll_checkboxes
{
    height: 120px;
    width: 200px;
    padding: 5px;
    overflow: auto;
    border: 1px solid #ccc;
}

.FormText
{
    FONT-SIZE: 11px;
    FONT-FAMILY: tahoma,sans-serif
}
</style>

<script language="javascript">

var color = 'White'; 

function changeColor(obj) 
{ 
    var rowObject = getParentRow(obj); 
    var parentTable = 
      document.getElementById("<%=chkList.ClientID%>"); 

    if(color == '') 
    {
        color = getRowColor(); 
    } 

    if(obj.checked) 
    {
        rowObject.style.backgroundColor = '#A3B1D8'; 
    }
    else 
    {
        rowObject.style.backgroundColor = color; 
        color = 'White'; 
    }

    // private method
    function getRowColor() 
    {
        if(rowObject.style.backgroundColor == 'White')
            return parentTable.style.backgroundColor; 
        else return rowObject.style.backgroundColor; 
    }
}

// This method returns the parent row of the object
function getParentRow(obj) 
{ 
    do 
    {
        obj = obj.parentElement; 
    }
    while(obj.tagName != "TR") 
    return obj; 
}

function TurnCheckBoixGridView(id)
{
    var frm = document.forms[0];

    for (i=0;i<frm.elements.length;i++)
    {
        if (frm.elements[i].type == "checkbox" && 
            frm.elements[i].id.indexOf("<%= chkList.ClientID %>") == 0)
        {
            frm.elements[i].checked = 
              document.getElementById(id).checked;
        }
    }
}

function SelectAll(id)
{
    var parentTable = document.getElementById("<%=chkList.ClientID%>"); 
    var color

    if (document.getElementById(id).checked)
    {
        color = '#A3B1D8'
    }
    else
    {
        color = 'White'
    }

    for (i=0;i<parentTable.rows.length;i++)
    {
        parentTable.rows[i].style.backgroundColor = color;
    }
    TurnCheckBoixGridView(id);
}

</script>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="scroll_checkboxes">
<asp:CheckBoxList Width="180px" ID="chkList" 
  runat="server" CssClass="FormText" 
  RepeatDirection="Vertical" RepeatColumns="1" 
  BorderWidth="0" Datafield="description" 
  DataValueField="value">
<asp:ListItem id="text1" Value="Select me 1" 
   onclick="changeColor(this);"></asp:ListItem>
<asp:ListItem id="text2" Value="Select me 2" 
   onclick="changeColor(this);"></asp:ListItem>
<asp:ListItem id="text3" Value="Select me 3" 
   onclick="changeColor(this);"></asp:ListItem>
<asp:ListItem id="text4" Value="Select me 4" 
   onclick="changeColor(this);"></asp:ListItem>
<asp:ListItem id="text5" Value="Select me 5" 
   onclick="changeColor(this);"></asp:ListItem>
<asp:ListItem id="text6" Value="Select me 6" 
   onclick="changeColor(this);"></asp:ListItem>
<asp:ListItem id="text7" Value="Select me 7" 
   onclick="changeColor(this);"></asp:ListItem>
<asp:ListItem id="text8" Value="Select me 8" 
   onclick="changeColor(this);"></asp:ListItem>
<asp:ListItem id="text9" Value="Select me 9" 
   onclick="changeColor(this);"></asp:ListItem>
<asp:ListItem id="text10" Value="Select me 10" 
   onclick="changeColor(this);"></asp:ListItem>
<asp:ListItem id="text11" Value="Select me 11" 
   onclick="changeColor(this);"></asp:ListItem>
<asp:ListItem id="text12" Value="Select me 12" 
   onclick="changeColor(this);"></asp:ListItem>
<asp:ListItem id="text13" Value="Select me 13" 
   onclick="changeColor(this);"></asp:ListItem>
</asp:CheckBoxList>
</div>
</div>
</form>
</body>
</html>

If you would need to load this list from a database, then you can easily bind the CheckBoxList to a DataSource object.

To add the JavaScript call to change the color, just use text1.Attributes.Add("onclick", "changeColor(this)"); in the code-behind for each ListItem control (you would need to use a loop).

Highlight the row in a GridView using the same Javascript code

You can use the same JavaScript for highlighting a row in a GridView; for example, let's say, you have a GridView with an ItemTemplate that has a CheckBox.

Adding onclick="changeColor(this)" to the checkbox will do the trick.

HTML
<ItemTemplate >
  <asp:CheckBox ID="chkSelect" runat="server" 
     Width = "24px" Visible = "true" 
     onclick="changeColor(this)" EnableViewState="True" />
  <asp:Label ID="HiddenShipmentID" runat="server" 
     visible = "false" 
     Text = '<%# Eval("shipment_id") %>'></asp:Label>
</ItemTemplate>.....

All you have to do is change the JavaScript to use the name of the GridView:

JavaScript
var parentTable = document.getElementById("<%=chkList.ClientID%>");

Change that to:

JavaScript
var parentTable = document.getElementById("<%=GridViewName.ClientID%>");

And that's all.

I hope you enjoyed this little trick. If you liked this idea, feel free to vote.

License

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


Written By
Web Developer TrafficTech
Canada Canada
Have technical skills that can be demonstrated and an ability to resolve complex problems quickly while working in a demanding, high pressure environment

Designs, plans, and coordinates software development work teams

Provides technical mentorship to project team members

Handles complex application features and technical designs for the development of new applications.

Write articles about ASP.net:
http://www.codeproject.com/KB/aspnet/SQLHelper20.aspx http://www.codeproject.com/KB/aspnet/DateAndTimePicker.aspx http://www.codeproject.com/KB/aspnet/SQLHelper20.aspx http://www.codeproject.com/KB/aspnet/WaitImageBoxWhilePagePost.aspx

Designs and implements the components, frameworks and layers required for complex application features

Understands and participate in all aspects of the Software Development Life Cycle

Relies on experience and judgment to plan and accomplish goals.

Ability to perform various programming activities (coding, testing, debugging, documenting, maintaining and supporting).

Ability to work independently with minimal supervision.

10 years’ experience in web software design and development.

SpecialtiesASP.net
SQL 2005
AJAX 1.0
Linq
C# 3.5
Microsoft Application Blocks
Java Script
Reporting Services
SQL SSIS
XML
Classic ASP

Comments and Discussions

 
QuestionWhat if RepeatColumns="4" Pin
chessdr2-Mar-21 5:40
chessdr2-Mar-21 5:40 
PraiseMultiSelect DropdownList Using jQuery in Asp.Net MVC and C#.Net Pin
Member 123685433-Mar-16 18:37
Member 123685433-Mar-16 18:37 
Questionadd textbox in this control Pin
Member 1075349317-Apr-14 19:55
Member 1075349317-Apr-14 19:55 
QuestionCan u make this control usable in gridview Pin
cooldiv4u11-Jun-13 0:00
cooldiv4u11-Jun-13 0:00 
QuestionYou really think its a DropDrown!?????? Pin
AliTopoli4-May-12 21:43
AliTopoli4-May-12 21:43 
AnswerRe: You really think its a DropDrown!?????? Pin
rperetz5-May-12 3:25
rperetz5-May-12 3:25 
QuestionCan you add searchable feature for this control Pin
raj chitty25-Apr-12 3:28
raj chitty25-Apr-12 3:28 
QuestionNot a Drop down Pin
Dov Miller15-Mar-12 0:29
Dov Miller15-Mar-12 0:29 
GeneralMulti-Select Drop Down List in ASP.NET Pin
Richard Xiong11-Aug-10 14:17
Richard Xiong11-Aug-10 14:17 
GeneralDesign View error in VS 2005 Pin
TonyOldskhl25-Mar-10 6:09
TonyOldskhl25-Mar-10 6:09 
GeneralNice Pin
samerh28-Jan-10 22:55
samerh28-Jan-10 22:55 
GeneralRe: Nice Pin
rperetz1-Feb-10 15:25
rperetz1-Feb-10 15:25 
GeneralMy vote of 1 Pin
Dave Kreskowiak28-Jan-10 11:44
mveDave Kreskowiak28-Jan-10 11:44 
GeneralRe: My vote of 1 Pin
rperetz1-Feb-10 15:14
rperetz1-Feb-10 15:14 

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.