Please refer this code
Database
Install Microsoft Northwind and Pubs Sample databases in SQL Server Management Studio
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
table
{
border: 1px solid #ccc;
}
table th
{
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
table th, table td
{
padding: 5px;
border-color: #ccc;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
url: "Default.aspx/GetCustomers",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
});
function OnSuccess(response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var customers = xml.find("Table");
var row = $("[id*=gvCustomers] tr:last-child").clone(true);
$("[id*=gvCustomers] tr").not($("[id*=gvCustomers] tr:first-child")).remove();
$.each(customers, function () {
var customer = $(this);
$("td", row).eq(1).html($(this).find("ContactName").text());
//Find TextBox using ClassName and set Value.
$(".ContactName", row).html($(this).find("CustomerID").text());
$("td", row).eq(2).html($(this).find("City").text());
$("[id*=gvCustomers]").append(row);
row = $("[id*=gvCustomers] tr:last-child").clone(true);
});
}
</script>
<script type="text/javascript">
$(function () {
$(".ContactName").click(function () {
$("#orders").show();
$.ajax({
type: "POST",
url: "Default.aspx/GetCustomerOrders",
data: '{customerId: "' + $(this).html() + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
},
success: function (response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var customers = xml.find("Table");
var row = $("[id*=gvOrders] tr:last-child").clone(true);
$("[id*=gvOrders] tr").not($("[id*=gvOrders] tr:first-child")).remove();
$.each(customers, function () {
var customer = $(this);
$("td", row).eq(0).html($(this).find("OrderID").text());
$("td", row).eq(1).html($(this).find("OrderDate").text());
$("td", row).eq(2).html($(this).find("ShipName").text());
$("[id*=gvOrders]").append(row);
row = $("[id*=gvOrders] tr:last-child").clone(true);
});
}
});
return false;
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField ItemStyle-Width="150px" HeaderText="Contact Name">
<ItemTemplate>
<asp:LinkButton runat="server" CssClass="ContactName" Text='<%# Eval("ContactName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
</Columns>
</asp:GridView>
<br />
<div id="orders" style="display: none;">
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="OrderId" HeaderText="OrderiD" />
<asp:BoundField ItemStyle-Width="150px" DataField="OrderDate" HeaderText="OrderDate" />
<asp:BoundField ItemStyle-Width="150px" DataField="ShipName" HeaderText="ShipName" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindDummyRow();
}
}
private void BindDummyRow()
{
DataTable dummy = new DataTable();
dummy.Columns.Add("CustomerID");
dummy.Columns.Add("ContactName");
dummy.Columns.Add("City");
dummy.Rows.Add();
gvCustomers.DataSource = dummy;
gvCustomers.DataBind();
DataTable dummy1 = new DataTable();
dummy1.Columns.Add("OrderId");
dummy1.Columns.Add("OrderDate");
dummy1.Columns.Add("ShipName");
dummy1.Rows.Add();
gvOrders.DataSource = dummy1;
gvOrders.DataBind();
}
[WebMethod]
public static string GetCustomers()
{
string query = "SELECT top 10 CustomerID, ContactName, City FROM Customers";
SqlCommand cmd = new SqlCommand(query);
return GetData(cmd).GetXml();
}
private static DataSet GetData(SqlCommand cmd)
{
string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
[WebMethod]
public static string GetCustomerOrders(string customerId)
{
string query = "SELECT OrderID, OrderDate, ShipName FROM [Orders] WHERE CustomerId = @CustomerId";
string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@CustomerId", customerId);
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds.GetXml();
}
}
}
}
}
Screenshot