Click here to Skip to main content
15,892,298 members
Articles / Programming Languages / C#

Multiselect DropDown ListBox Control for Web Applications

,
Rate me:
Please Sign up or sign in to vote.
4.94/5 (14 votes)
3 Dec 2008CPOL5 min read 107K   3.1K   41  
Enables the user to select multiple options in a drop down
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace MultiSelect_DropDown
{
    public partial class _Default : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
               InitialiseDropDownControls();
               LoadData();
            }
            //Hooking the selected index changed event.
            multiselect_Juices.OnMSDDSelectedIndexChanged += new Controls.MultiselectDropDownListBox.OnList_SelectedIndexChangedEventHandler(multiselect_Juices_OnMSDDSelectedIndexChanged);
           
        }

        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
            multiselect_foodItems.Width = 160;
            multiselect_foodItems.Height = 15;
            // Adding "onclick" events to the items in the listbox.
            //multiselect_foodItems.AddAttribute("onclick", "alert('test');");
           
            multiselect_Juices.Width = 160;
            multiselect_Juices.Height= 15;
       

        }

        void multiselect_Juices_OnMSDDSelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.multiselect_Juices.SelectedItems.Count > 0)
            {
                this.lblSelectedItem.BackColor = System.Drawing.Color.Aqua;
                this.lblSelectedItem.Text = this.multiselect_Juices.SelectedText;
            }
            else
            {
                this.lblSelectedItem.BackColor = System.Drawing.Color.Beige;
                this.lblSelectedItem.Text = "No Juice Selected";
            }
        }

        

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

        void multiselect_Juices_OnList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Write(multiselect_Juices.SelectedItems.ToString());
            this.lblSelectedItem.Text = multiselect_Juices.SelectedText;
        }

        private DataSet GetFoodItems()
        {
            DataSet dsFoodItems = new DataSet();
            dsFoodItems.Tables.Add(new DataTable("tblFoodItems"));
            dsFoodItems.Tables[0].Columns.Add("FoodItems");
            dsFoodItems.Tables[0].Rows.Add("Orange");
            dsFoodItems.Tables[0].Rows.Add("Garlic bread");
            dsFoodItems.Tables[0].Rows.Add("Fruit Salad");
            dsFoodItems.Tables[0].Rows.Add("Pasta");
            dsFoodItems.Tables[0].Rows.Add("French Fries");
            dsFoodItems.Tables[0].Rows.Add("Veg Pizza");
            dsFoodItems.Tables[0].Rows.Add("Chicken Burger");
            dsFoodItems.Tables[0].Rows.Add("Veg Burger");
            dsFoodItems.Tables[0].Rows.Add("Chicken Nuggets");
            dsFoodItems.Tables[0].Rows.Add("French Fries");
            dsFoodItems.Tables[0].Rows.Add("Fruit Salad");
            dsFoodItems.Tables[0].Rows.Add("Pasta");
            dsFoodItems.Tables[0].Rows.Add("French Fries");
            dsFoodItems.Tables[0].Rows.Add("Fruit Salad");
            dsFoodItems.Tables[0].Rows.Add("Pasta");
            dsFoodItems.Tables[0].Rows.Add("French Fries");
            return dsFoodItems;
        }

        protected void multiselect_foodItems_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

      

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Written By
Software Developer iNautix Technologies India Pvt Ltd
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions