Categories
Asp, Asp.net

Checkbox Control In Dropdownlist Control In Asp.Net

Checkbox Control In Dropdownlist Control In Asp.Net

In this post we will learn How To Use Checkbox Control In Dropdownlist Control In Asp.Net, or you can say DropDownCheckBoxes in CheckBoxList control in ASP.NET.

ASP.NET provides DropDownList and CheckBoxList server controls  but there is no controls available for DropDownList with multi select option.

DropDownCheckBoxes is a server control provides functionality to select multiple items in DropDownList. ASP.NET Web Forms 3.5, 4.0, can work with both synchronous postbacks and asynchronous when the control is placed within UpdatePanel control.

You can download DropDownCheckBoxes.dll by following the link given.http://dropdowncheckboxes.codeplex.com/releases/view/70874.

Download the above mention DLL file from given link and then Add Reference > Select DLL file and use it.

Web.Config Changes(Register Component):

<system.web>
<pages>
<controls>
<add tagPrefix=”uasp” namespace=”Saplin.Controls” assembly= “DropDownCheckBoxes”/>
</controls>
</pages>
</system.web>

Design View:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Checkbox Control In Dropdownlist Control In Asp.Net</title>
<style type="text/css">
body
 {
 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 18px;
 font-family:Calibri;
 }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Checkbox Control In Dropdownlist Control In Asp.Net</h2>
Select Country:<uasp:DropDownCheckBoxes ID="ddchk" runat="server">
<Style SelectBoxWidth="200" DropDownBoxBoxWidth="200" DropDownBoxBoxHeight="200" /> 
<Texts SelectBoxCaption="Select Country" />
</uasp:DropDownCheckBoxes>

<asp:Button ID="submit" runat="server" Text="Submit" OnClick="submit_Click" />

<br /><br />
<asp:Label ID="selectedcontry" runat="server"></asp:Label>
<br /><br />
<br /><br />
<br /><br />
<br /><br />
All rights reserved by <a href="http://www.hightechnology.in">www.Hightechnology.in</a>
| Hosting partner <a href="http://www.grootstech.com" target="_blank">Grootstech</a>   
</div>
</form>
</body>
</html>

Code:

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    protected void BindData()
    {
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=hightechnology;Integrated Security=True");
        DataSet ds = new DataSet();
        string cmdstr = "select CountryID,Country from Country_list";
        SqlDataAdapter adp = new SqlDataAdapter(cmdstr, conn);
        adp.Fill(ds);

        if (ds.Tables[0].Rows.Count > 0)
        {
            ddchk.DataSource = ds.Tables[0];
            ddchk.DataTextField = "Country";
            ddchk.DataValueField = "CountryID";
            ddchk.DataBind();
        }
    }
    protected void submit_Click(object sender, EventArgs e)
    {
        List<String> ContryName_list = new List<string>();
        foreach (System.Web.UI.WebControls.ListItem item in ddchk.Items)
        {
            if (item.Selected)
            {
                ContryName_list.Add(item.Text);
            }
            selectedcontry.Text = "Country Name: " + String.Join(",", ContryName_list.ToArray());
        }
    }
}