Displaying Nested Master/Details Forms
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
protected void grdProductCategories_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int categoryId = (int)DataBinder.Eval(e.Row.DataItem,"Id");
SqlDataSource srcProducts = (SqlDataSource)e.Row.FindControl("srcProducts");
srcProducts.SelectParameters["CategoryId"].DefaultValue = categoryId.ToString();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<form id="form1" runat="server">
<div>
<asp:GridView
id="grdProducts"
DataSourceID="srcProductCategories"
OnRowDataBound="grdProductCategories_RowDataBound"
AutoGenerateColumns="false"
CssClass="categories"
ShowHeader="false"
GridLines="none"
Runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<h1><%# Eval("Name") %></h1>
<asp:GridView
id="grdProducts"
DataSourceId="srcProducts"
CssClass="Products"
GridLines="none"
Runat="server" />
<asp:SqlDataSource
id="srcProducts"
ConnectionString="<%$ ConnectionStrings:Products %>"
SelectCommand="SELECT Title,Director FROM Products
WHERE CategoryId=@CategoryId"
Runat="server">
<SelectParameters>
<asp:Parameter Name="CategoryId" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource
id="srcProductCategories"
ConnectionString="<%$ ConnectionStrings:Products %>"
SelectCommand="SELECT Id,Name FROM ProductCategories"
Runat="server" />
</div>
</form>
</body>
</html>
File: Web.config
<configuration>
<connectionStrings>
<add name="Products"
connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
</connectionStrings>
</configuration>
Related examples in the same category