Hi hansheung,
I have created a working 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
VB.Net
IC_MasterPage.master
<%@ Master Language="VB" CodeFile="IC_MasterPage.master.vb" Inherits="VB_IC_MasterPage" %>
<!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></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
VB.aspx
<%@ Page Title="" Language="VB" MasterPageFile="~/VB/IC_MasterPage.master" AutoEventWireup="false"
CodeFile="VB.aspx.vb" Inherits="VB_VB" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<asp:TextBox runat="server" ID="txtCountry" />
<asp:Button ID="btnSearch" Text="Search" runat="server" />
<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>
<div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id$=btnSearch]").on("click", function () {
$.ajax({
type: "POST",
url: '<%= ResolveUrl("VB.aspx/GetCustomers") %>',
data: '{searchTerm: "' + $('[id$=txtCountry]').val() + '"}',
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); }
});
return false;
});
});
</script>
</div>
</asp:Content>
VB.aspx.vb
Imports System.Data.SqlClient
Imports System.Data
Partial Class VB_VB
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
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(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 LIKE '") & 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
End Class
C#
IC_MasterPage.master
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="IC_MasterPage.master.cs"
Inherits="CS_IC_MasterPage" %>
<!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></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
CS.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/CS/IC_MasterPage.master" AutoEventWireup="true"
CodeFile="CS.aspx.cs" Inherits="CS_CS" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<asp:TextBox runat="server" ID="txtCountry" />
<asp:Button ID="btnSearch" Text="Search" runat="server" />
<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>
<div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id$=btnSearch]").on("click", function () {
$.ajax({
type: "POST",
url: '<%= ResolveUrl("CS.aspx/GetCustomers") %>',
data: '{searchTerm: "' + $('[id$=txtCountry]').val() + '"}',
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); }
});
return false;
});
});
</script>
</div>
</asp:Content>
CS.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public partial class CS_CS : System.Web.UI.Page
{
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 LIKE '" + 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();
}
}