Click here to Skip to main content
Click here to Skip to main content

Easiest way to Create a Multiselect Dropdown in ASP.NET 4.0 using C#

By , 22 Mar 2013
 

Multiselect_Dropdownlist/multi2.PNG

Introduction

In this article, I will show you how to create a multi-select control in ASP.NET. I will keep this article short and sweet so you can just use the code in your applications. Previously I referred to many codes for a multiselect dropdown but I found they were all a little complex to implement. So I decided to try it on my own. Let's see how I implemented this.

Background

First, you must install AJAX controls in Visual Studio. I am using VS2010. This is because I am using the popup control extender to implement this control.

Using the code

Place a textbox in Design view and assign the popup extender for that textbox. An important thing to note here is you must place your textbox control within the UpdatePanel.

Afterwards, you create a panel and within this panel, you put a checkboxlist. You then assign this panel to the PopupControlID property of the popup extender.

Now take a look at this code:

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

<%@ Register Assembly="AjaxControlToolkit" 
  Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ToolkitScriptManager runat="server">
        </asp:ToolkitScriptManager>

        <asp:UpdatePanel ID="updatepanel1" runat="server">

            <ContentTemplate>

                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

                <asp:PopupControlExtender 
                    ID="TextBox1_PopupControlExtender" runat="server"
                    Enabled="True" ExtenderControlID="" 
                    TargetControlID="TextBox1" 
                    PopupControlID="Panel1" OffsetY="22">
                </asp:PopupControlExtender>

                <asp:Panel ID="Panel1" runat="server" 
                     Height="116px" Width="145px" 
                     BorderStyle="Solid" BorderWidth="2px" 
                     Direction="LeftToRight"
                     ScrollBars="Auto" BackColor="#CCCCCC" 
                     Style="display: none">
                   
                    <asp:CheckBoxList ID="CheckBoxList1" runat="server" 
                          DataSourceID="SqlDataSource1" DataTextField="holiday_name"
                         DataValueField="holiday_name" AutoPostBack="True"
                        OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
                    </asp:CheckBoxList>

                    <asp:SqlDataSource ID="SqlDataSource1" 
                          runat="server" ConnectionString="<%$ 
                          ConnectionStrings:employeedbConnectionString %>"
                        SelectCommand="SELECT [holiday_name] FROM [tblholidaymas]"> 
                     </asp:SqlDataSource>
                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

Here, you select the datasource from your SQL Server database.

Multiselect_Dropdownlist/multi1.PNG

Finally, we need to do one more modification in the code-behind. Each time we click the checkboxlist, the selected value will be added to the textbox control. So in the selected index changed event, you will place this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    
    }
  
    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string name = "";
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
            {
                name += CheckBoxList1.Items[i].Text + ",";
            }
        }
        TextBox1.Text = name;
    }
}

That's all.

License

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

About the Author

CS1401
Software Developer (Junior) CS1401
India India
Member


Any fool can write code that a computer can understand. Good programmers write code that humans can understand.


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionhow do you load existing items as comma separated strings from a databasememberMember 99592122 Apr '13 - 0:12 
how do you load existing items as comma separated strings from a database? e.g. you made some selection and saved the text box to the database, next time you want to load the same item and display it in the dropdown. Is this automatically picked up?
 
Thanks.
AnswerRe: how do you load existing items as comma separated strings from a databasememberCS14017 Apr '13 - 0:23 
Sorry, Nope. You Need To Write Separate Logic based on Your Requirement. Smile | :)
...

GeneralMy vote of 5memberWebBHTAN23 Mar '13 - 17:31 
Alternative way
GeneralCheck allmemberelaya_627 Jun '12 - 5:36 
HI ..
How to add check all check box ?
GeneralRe: Check allmemberCS14011 Jul '12 - 3:27 
you should use one checkbox control above the checkbox list. And trigger the
single checkbox control event for check all the boxes in checkboxlist. Big Grin | :-D
...

GeneralMy vote of 5memberGariel Martini9 Feb '12 - 5:30 
goood
GeneralRe: My vote of 5memberCS140117 Feb '12 - 19:10 
thank u.. Big Grin | :-D
...

GeneralMy vote of 3 [modified]mvpMarcelo Ricardo de Oliveira10 Oct '11 - 0:06 
Nice.
 
Despite the subject of my message, actually I voted 4, not 3. I think it deserves a 4.
 
Okay, now I see you're downvoting my article because I gave you a 4, so you had to counter-attack. Not only that, my article also got many other downvotes in the latest hours. BTW, your article strangely got more than 15 votes in just a few hours in the monthly competition, are you aware of that?
 
marcelo

modified 10 Oct '11 - 11:03.

GeneralRe: My vote of 3memberCS140110 Oct '11 - 18:34 
Hey i also put 4 for you. But dont know how to changed it like before happened to you. i think some kind of error occurring. Ya i also noticed, i think many voters are understand my algorithm and can realize the efficiency of this one.Cool | :cool:
...

GeneralMy vote of 5memberAnurag Gandhi7 Oct '11 - 1:14 
Good One.

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 23 Mar 2013
Article Copyright 2011 by CS1401
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid