Hi kats9993,
I have created sample that full fill your requirement.
For this sample I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$(".test").on("click", function () {
var isChecked = $(this).find('input[type=checkbox]').is(":checked");
var text;
if (isChecked) {
text = $(this)[0].textContent;
}
else {
text = "";
}
$.ajax({
type: "POST",
url: "CS.aspx/GetCustomers",
data: '{searchTerm: "' + text + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var row;
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var customers = xml.find("Customers");
if (row == null) {
row = $("[id*=gvCustomers] tr:last-child").clone(true);
}
$("[id*=gvCustomers] tr").not($("[id*=gvCustomers] tr:first-child")).remove();
if (customers.length > 0) {
$.each(customers, function () {
$("td", row).eq(0).html($(this).find("ContactName").text());
$("td", row).eq(1).html($(this).find("CustomerID").text());
$("td", row).eq(2).html($(this).find("City").text());
$("td", row).eq(3).html($(this).find("Country").text());
$("[id*=gvCustomers]").append(row);
row = $("[id*=gvCustomers] tr:last-child").clone(true);
});
} else {
var empty_row = row.clone(true);
$("td:first-child", empty_row).attr("colspan", $("td", row).length);
$("td:first-child", empty_row).attr("align", "center");
$("td:first-child", empty_row).html("No records found for the search criteria.");
$("td", empty_row).not($("td:first-child", empty_row)).remove();
$("[id*=gvCustomers]").append(empty_row);
}
},
failure: function (response) { alert(response.d); },
error: function (response) { alert(response.d); }
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="CheckBox1" class="test" runat="server" Text="USA" Value="USA" />
<asp:CheckBox ID="CheckBox2" class="test" runat="server" Text="UK" Value="UK" />
<asp:CheckBox ID="CheckBox3" class="test" runat="server" Text="Germany" Value="Germany" />
<asp:CheckBox ID="CheckBox4" class="test" runat="server" Text="France" Value="France" />
<br />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="Contact Name" />
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataSet ds = new DataSet())
{
sda.Fill(ds, "Customers");
gvCustomers.DataSource = ds;
gvCustomers.DataBind();
}
}
}
}
[System.Web.Services.WebMethod]
public static string GetCustomers(string searchTerm = "")
{
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
string query = "SELECT * FROM Customers";
if (!string.IsNullOrEmpty(searchTerm))
{
query += " WHERE Country = '" + searchTerm + "'";
}
SqlCommand cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, "Customers");
return ds.GetXml();
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
BindGridView()
End If
End Sub
Private Sub BindGridView()
Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand("SELECT * FROM Customers", con)
Using sda As New SqlDataAdapter(cmd)
Using ds As New DataSet()
sda.Fill(ds, "Customers")
gvCustomers.DataSource = ds
gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
<System.Web.Services.WebMethod> _
Public Shared Function GetCustomers(Optional searchTerm As String = "") As String
Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As New SqlConnection(strConnString)
Dim query As String = "SELECT * FROM Customers"
If Not String.IsNullOrEmpty(searchTerm) Then
query += (Convert.ToString(" WHERE Country = '") & searchTerm) + "'"
End If
Dim cmd As New SqlCommand(query, con)
cmd.CommandType = CommandType.Text
Dim sda As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
sda.Fill(ds, "Customers")
Return ds.GetXml()
End Function