Hi itsme,
Refer below sample.
HTML
<div class="dropdown ">
<button class="dropbtn">
--Country--</button>
<div class="dropdown-content" id="drp" runat="server">
<asp:CheckBox Text="India" runat="server" ID="chkInd" />
<br />
<asp:CheckBox Text="United States" runat="server" ID="chkUSA" />
<br />
<asp:CheckBox Text="France" runat="server" ID="chkFrance" />
<br />
<asp:CheckBox Text="Russia" runat="server" ID="chkRussia" />
<br />
<asp:Button Text="Search" runat="server" OnClick="FilterData" />
<br />
<asp:GridView runat="server" ID="gvCustomers" />
</div>
</div>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = GetData("SELECT CustomerId, Name,Country FROM Customers");
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
private static DataTable GetData(string query)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
protected void FilterData(object sender, EventArgs e)
{
string query = "SELECT CustomerId, Name,Country FROM Customers";
query += " WHERE Country IN (";
foreach (CheckBox chk in drp.Controls.OfType<CheckBox>())
{
if (chk.Checked)
{
query += "'" + chk.Text + "'" + ",";
}
}
query = query.Substring(0, query.Length - 1);
query += ")";
DataTable dt = GetData(query);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = GetData("SELECT CustomerId, Name,Country FROM Customers")
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End If
End Sub
Private Shared Function GetData(ByVal query As String) As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand(query, con)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Return dt
End Function
Protected Sub FilterData(ByVal sender As Object, ByVal e As EventArgs)
Dim query As String = "SELECT CustomerId, Name,Country FROM Customers"
query += " WHERE Country IN ("
For Each chk As CheckBox In drp.Controls.OfType(Of CheckBox)()
If chk.Checked Then
query += "'" & chk.Text & "'" & ","
End If
Next
query = query.Substring(0, query.Length - 1)
query += ")"
Dim dt As DataTable = GetData(query)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Sub
Screenshot