Hi Waghmare,
Refer below sample.
Database
For this example 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.8.3/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css" />
<script type="text/javascript" src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js"></script>
<script type="text/javascript">
$(function () {
$('[id*=lstCustomers]').multiselect({
includeSelectAllOption: true,
enableFiltering: true,
filterPlaceholder: 'Search',
enableCaseInsensitiveFiltering: true,
dropRight: true
});
});
</script>
<div class="col-md-3">
<label class="control-label" style="margin-top: 10px;">
Customers</label>
<asp:ListBox ID="lstCustomers" runat="server" SelectionMode="Multiple" class="form-control">
</asp:ListBox>
<asp:Button Text="Submit" OnClick="OnSubmit" runat="server" CssClass="btn btn-default" />
</div>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lstCustomers.DataSource = GetData("SELECT TOP 10 CustomerID,ContactName FROM Customers");
lstCustomers.DataTextField = "ContactName";
lstCustomers.DataValueField = "CustomerID";
lstCustomers.DataBind();
}
}
protected void OnSubmit(object sender, EventArgs e)
{
string message = "";
foreach (ListItem item in lstCustomers.Items)
{
if (item.Selected)
{
message += item.Text + " " + item.Value + "\\n";
}
}
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + message + "');", true);
}
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
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);
return ds;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
lstCustomers.DataSource = GetData("SELECT TOP 10 CustomerID,ContactName FROM Customers")
lstCustomers.DataTextField = "ContactName"
lstCustomers.DataValueField = "CustomerID"
lstCustomers.DataBind()
End If
End Sub
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
Dim message As String = ""
For Each item As ListItem In lstCustomers.Items
If item.Selected Then
message += item.Text & " " + item.Value & "\n"
End If
Next
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "alert", "alert('" & message & "');", True)
End Sub
Private Function GetData(ByVal query As String) As DataSet
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
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)
Return ds
End Using
End Using
End Using
End Function
Screenshot