Hi davelhw,
Please take reference the below code and correct your code.
Database
For this sample I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
<table class="Grid" cellspacing="0" rules="all" border="1">
<tr>
<th scope="col">
</th>
<th scope="col" style="width: 150px">
Contact Name
</th>
<th scope="col" style="width: 150px">
City
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<img alt="" style="cursor: pointer" src="images/plus.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display: none">
<asp:Repeater ID="rptOrders" runat="server">
<HeaderTemplate>
<table class="ChildGrid" cellspacing="0" rules="all" border="1">
<tr>
<th scope="col" style="width: 150px">
Order Id
</th>
<th scope="col" style="width: 150px">
Date
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:TextBox ID="txtOrderId" runat="server" Text='<%#Eval("OrderId") %>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtShipCountry" runat="server" Text='<%#Eval("ShipCountry") %>'></asp:TextBox>
</td>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="Save" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</asp:Panel>
<asp:HiddenField ID="hfCustomerId" runat="server" Value='<%# Eval("CustomerId") %>' />
</td>
<td>
<asp:Label ID="lblContactName" runat="server" Text='<%# Eval("ContactName") %>' />
</td>
<td>
<asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Label ID="lblOrderId" runat="server" ForeColor="Red"></asp:Label>
<asp:Label ID="lblShipCountry" runat="server" ForeColor="Red"></asp:Label>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("body").on("click", "[src*=plus]", function () {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
$(this).attr("src", "images/minus.png");
});
$("body").on("click", "[src*=minus]", function () {
$(this).attr("src", "images/plus.png");
$(this).closest("tr").next().remove();
});
</script>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
rptCustomers.DataSource = GetData("SELECT TOP 3 * FROM Customers");
rptCustomers.DataBind();
}
}
private static DataTable GetData(string query)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = query;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string customerId = (e.Item.FindControl("hfCustomerId") as HiddenField).Value;
Repeater rptOrders = e.Item.FindControl("rptOrders") as Repeater;
rptOrders.DataSource = GetData(string.Format("SELECT TOP 3 OrderId,ShipCountry FROM Orders WHERE CustomerId='{0}'", customerId));
rptOrders.DataBind();
}
}
protected void Save(object sender, EventArgs e)
{
Button btn = (sender as Button);
TextBox txtOrderId = btn.FindControl("txtOrderId") as TextBox;
TextBox txtShipCountry = btn.FindControl("txtShipCountry") as TextBox;
lblOrderId.Text = "Order ID :" + txtOrderId.Text.Split(',')[1];
lblShipCountry.Text = "Ship Country :" + txtShipCountry.Text.Split(',')[1];
}
Screenshot