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:

01<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
02 
03<!DOCTYPE html>
04 
06<head runat="server">
07<title>Checkbox Control In Dropdownlist Control In Asp.Net</title>
08<style type="text/css">
09body
10 {
11 width: 980px;
12 margin: 0px auto;
13 text-align: center;
14 padding-top: 50px;
15 font-size: 18px;
16 font-family:Calibri;
17 }
18</style>
19</head>
20<body>
21<form id="form1" runat="server">
22<div>
23<h2>Checkbox Control In Dropdownlist Control In Asp.Net</h2>
24Select Country:<uasp:DropDownCheckBoxes ID="ddchk" runat="server">
25<Style SelectBoxWidth="200" DropDownBoxBoxWidth="200" DropDownBoxBoxHeight="200" />
26<Texts SelectBoxCaption="Select Country" />
27</uasp:DropDownCheckBoxes>
28 
29<asp:Button ID="submit" runat="server" Text="Submit" OnClick="submit_Click" />
30 
31<br /><br />
32<asp:Label ID="selectedcontry" runat="server"></asp:Label>
33<br /><br />
34<br /><br />
35<br /><br />
36<br /><br />
37All rights reserved by <a href="http://www.hightechnology.in">www.Hightechnology.in</a>
38| Hosting partner <a href="http://www.grootstech.com" target="_blank">Grootstech</a>  
39</div>
40</form>
41</body>
42</html>

Code:

01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Web;
05using System.Web.UI;
06using System.Web.UI.WebControls;
07using System.Data;
08using System.Data.SqlClient;
09 
10public partial class _Default : System.Web.UI.Page
11{
12    protected void Page_Load(object sender, EventArgs e)
13    {
14        if (!IsPostBack)
15        {
16            BindData();
17        }
18    }
19    protected void BindData()
20    {
21        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=hightechnology;Integrated Security=True");
22        DataSet ds = new DataSet();
23        string cmdstr = "select CountryID,Country from Country_list";
24        SqlDataAdapter adp = new SqlDataAdapter(cmdstr, conn);
25        adp.Fill(ds);
26 
27        if (ds.Tables[0].Rows.Count > 0)
28        {
29            ddchk.DataSource = ds.Tables[0];
30            ddchk.DataTextField = "Country";
31            ddchk.DataValueField = "CountryID";
32            ddchk.DataBind();
33        }
34    }
35    protected void submit_Click(object sender, EventArgs e)
36    {
37        List<String> ContryName_list = new List<string>();
38        foreach (System.Web.UI.WebControls.ListItem item in ddchk.Items)
39        {
40            if (item.Selected)
41            {
42                ContryName_list.Add(item.Text);
43            }
44            selectedcontry.Text = "Country Name: " + String.Join(",", ContryName_list.ToArray());
45        }
46    }
47}