Description:-
In
this example I am explain that how to use DropDownCheckBoxes or Drop Down
CHeckBoxList control or select multiple Items from DropdownList in ASP.NET.
In
.NET DropDownList does not provide
facility with multi select Items in DropdownList. So to overcome this :-
DropDownCheckBoxes is a server control provides functionality to select
multiple items from DropDownList.You can download
DropDownCheckBoxes.dll
by following the link given below.
http://dropdowncheckboxes.codeplex.com/releases/view/70874
there
is a requirement in my application to user can select multiple items from dropdownlist but
Asp.Net does not provide this facility and so this reason I have Developed a
Custom class for DropdownList that can give facility to user to select multiple
items from DropdownList with CheckBox Facility.
First
you have add References of DropDownCheckBoxes.dll
in your website and then you can easily create a DropdownList with multiple
items selection.
to download complete example click below download image link
Export Gridview to Excel :- Gridview to Excel
Insert,Update,Delete in ModalPopup CRUD operation in ModalPopup
Read and Write in Text File in asp.Net Read and Write File in Asp.Net
Default.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="DropDownCheckBoxes" Namespace="Saplin.Controls" 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>Select Multiple Item From Dropdownlist
throgh checkbox in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownCheckBoxes ID="ddlcountrychk1" runat="server"
AddJQueryReference="True" UseButtons="True" UseSelectAllNode="True">
<Style SelectBoxWidth="200" DropDownBoxBoxWidth="200" DropDownBoxBoxHeight="130" />
<Texts SelectBoxCaption="Select multipe Country" />
</asp:DropDownCheckBoxes>
<asp:Label ID="lblcountry_id" runat="server"></asp:Label><br />
<asp:Label ID="lblcountry_name" runat="server"></asp:Label>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</div>
</form>
</body>
</html>
Default.aspx.cs:-
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
{
SqlConnection conn = new SqlConnection("Data
Source=SQLDB;Initial Catalog=Demo;User ID=Demoh;Password=Demo1@");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
DataSet ds = new DataSet();
string cmdstr = "select
country_id,country_name from country11";
SqlDataAdapter adp = new SqlDataAdapter(cmdstr, conn);
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
ddlcountrychk1.DataSource =
ds.Tables[0];
ddlcountrychk1.DataTextField = "country_name";
ddlcountrychk1.DataValueField = "country_id";
ddlcountrychk1.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
List<String> CountryID_list = new List<string>();
List<String> CountryName_list = new List<string>();
foreach (System.Web.UI.WebControls.ListItem item in ddlcountrychk1.Items)
{
if (item.Selected)
{
CountryID_list.Add(item.Value);
CountryName_list.Add(item.Text);
}
lblcountry_id.Text = "Country ID: " +
String.Join(",",
CountryID_list.ToArray());
lblcountry_name.Text = "Country Name: " +
String.Join(",",
CountryName_list.ToArray());
}
}
}
showing one image and posting code it shows another result can you please provide above example code that is showing the all selected items in texts selectioncaption box.Thank you
ReplyDelete