Hi zeynab,
Check this example. Now please take its reference and correct your code.
Database
For this sample I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
HTML
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<script type="text/javascript">
$(function () {
$(".js-example-placeholder-single").select2({
placeholder: "Select",
allowClear: false
});
$('#ddl1').on('change', function () {
$('#<%=hfSelected.ClientID%>').val($(this).val());
});
});
</script>
<asp:DropDownList ID="ddl1" Width="300px" runat="server" multiple="multiple" CssClass="form-control js-example-placeholder-single"
ToolTip="Select ">
</asp:DropDownList>
<asp:HiddenField ID="hfSelected" runat="server" />
<asp:Button Text="Selected Country" runat="server" OnClick="GetSelected" />
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateDropDownList();
}
}
private void PopulateDropDownList()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT DISTINCT Country FROM Customers WHERE Country IS NOT NULL";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
ddl1.DataSource = ds;
ddl1.DataTextField = "Country";
ddl1.DataValueField = "Country";
ddl1.DataBind();
}
}
}
}
protected void GetSelected(object sender, EventArgs e)
{
string selectedCountries = hfSelected.Value;
string countries = "";
for (int i = 0; i < selectedCountries.Split(',').Length; i++)
{
countries += selectedCountries.Split(',')[i] + " \\n";
}
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Selected Countries: \\n" + countries + "')", true);
ddl1.SelectedIndex = -1;
hfSelected.Value = "";
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
PopulateDropDownList()
End If
End Sub
Private Sub PopulateDropDownList()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT DISTINCT Country FROM Customers WHERE Country IS NOT NULL"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As DataSet = New DataSet()
sda.Fill(ds)
ddl1.DataSource = ds
ddl1.DataTextField = "Country"
ddl1.DataValueField = "Country"
ddl1.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub GetSelected(ByVal sender As Object, ByVal e As EventArgs)
Dim selectedCountries As String = hfSelected.Value
Dim countries As String = ""
For i As Integer = 0 To selectedCountries.Split(","c).Length - 1
countries += selectedCountries.Split(","c)(i) & " \n"
Next
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('Selected Countries: \n" & countries & "')", True)
ddl1.SelectedIndex = -1
hfSelected.Value = ""
End Sub
Screenshot
