Click here to Skip to main content
15,892,537 members
Articles / Web Development / XHTML

Multiselect Dropdown for Web Applications

Rate me:
Please Sign up or sign in to vote.
4.83/5 (82 votes)
17 Dec 2008CPOL2 min read 299.5K   6.7K   168  
How to select multiple values from 'a dropdown look & feel' in a Web application
using System;
using System.Data;
using System.Configuration;
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;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lstMultipleValues.Attributes.Add("onclick", "FindSelectedItems(this," + txtSelectedMLValues.ClientID + ");");
            SetMultiValue();
            lblDescription.Text = "On click of dropdown, the multi select option will appear.<br /> Once you remove focus from the dropdown"
            + " area it will get auto collapsed. <br />In case needed, you can go ahead and close it with the close ('X') provided at the bottom.<br />";
        }
    }

    protected void SetMultiValue()
    {
        DataTable dt = new DataTable("Table1");
        
        DataColumn dc1 = new DataColumn("Text");
        DataColumn dc2 = new DataColumn("Value");
        dt.Columns.Add(dc1);
        dt.Columns.Add(dc2);

        //To get enough data for scroll
        dt.Rows.Add("Bangalore", 1);
        dt.Rows.Add("Kolkata", 2);
        dt.Rows.Add("Pune", 3);
        dt.Rows.Add("Mumbai", 4);
        dt.Rows.Add("Noida", 5);
        dt.Rows.Add("Gurgaon", 6);
        dt.Rows.Add("Hyderabad", 7);
        dt.Rows.Add("Chennai", 8);
        dt.Rows.Add("Jaipur", 8);
        dt.Rows.Add("Patna", 8);
        dt.Rows.Add("Ranchi", 8);

        lstMultipleValues.DataSource = dt;
        lstMultipleValues.DataTextField = "Text";
        lstMultipleValues.DataValueField = "Value";
        lstMultipleValues.DataBind();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        // Text showed that were selected
        lblTextSelected.Text = "Submitted Values: " + txtSelectedMLValues.Value;

        // One can loop through the checkboxlist control to get the Values too for the text selected if required.
    }
}

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
Architect Intuit India
India India


A software professional for more than 17+ years building solutions for Web and Desktop applications.

Currently working at Intuit India.

Website: Learn By Insight
Github: Sandeep Mewara
LinkedIn: Sandeep Mewara

Strongly believe in learning and sharing knowledge.



Comments and Discussions