<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>FormView with Nested GridView</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server">
void getImageUrl ( Object src, EventArgs e ) {
DataRowView dataRow = ( DataRowView ) categoriesFormView.DataItem;
string catName = dataRow [ "CategoryName" ].ToString ( ).Replace ( "/", " " );
Image img = ( Image ) categoriesFormView.FindControl ( "img" );
img.ImageUrl = string.Format ( "/shared/images/nwind/{0}.png", catName );
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>FormView with Nested GridView</h2></div>
<hr size=1 width=92%>
<form runat="server">
<div align="center">
<asp:formview id="categoriesFormView" runat="server"
datasourceid="categories"
datakeynames="categoryid"
width=90% cellpadding=5
gridlines="both"
allowpaging
ondatabound="getImageUrl">
<headerstyle backcolor="darkslategray" forecolor="khaki"
height="18pt" font-bold />
<rowstyle backcolor="ghostwhite" font-size="9pt"
verticalalign="top" />
<pagersettings
mode="nextprevious"
nextpagetext="Next" previouspagetext="Back" />
<pagerstyle horizontalalign="right" />
<headertemplate>
Northwind Traders Products Inventory</span>
</headertemplate>
<itemtemplate>
<table width=100% cellpadding=5>
<tr align="center">
<td>
<asp:image id="img" runat="server" />
<div id="msg"><%# Eval ( "CategoryName" ) %></div></td>
<td>
<asp:gridview id="categoryDetailsGrid" runat="server"
datasourceid="categoryInfo"
width=96% font-size="10pt" cellpadding=5
gridlines="horizontal" autogeneratecolumns=false>
<headerstyle height="18pt" font-bold
backcolor="steelblue"
forecolor="mintcream"
horizontalalign="center" />
<rowstyle backcolor="whitesmoke" verticalalign="top" />
<columns>
<asp:boundfield headertext="Product"
datafield="productname" />
<asp:boundfield headertext="Packaging"
datafield="quantityperunit" />
<asp:boundfield headertext="In Stock"
datafield="unitsinstock"
itemstyle-horizontalalign="right"/>
<asp:boundfield headertext="Unit Price"
datafield="unitprice"
htmlencode=false
dataformatstring="{0:c}"
itemstyle-horizontalalign="right"/>
</columns>
</asp:gridview>
</td></tr>
</table>
</itemtemplate>
</asp:formview>
<asp:sqldatasource id="categories" runat="server"
selectcommand = "SELECT * FROM Categories"
connectionstring="<%$ ConnectionStrings:aspnet %>" />
<asp:sqldatasource id="categoryInfo" runat="server"
selectcommand = "SELECT * FROM Products WHERE CategoryId = @CategoryId"
connectionstring="<%$ ConnectionStrings:aspnet %>">
<selectparameters>
<asp:controlparameter name="CategoryId" controlid="categoriesFormView"
propertyname="selectedvalue" />
</selectparameters>
</asp:sqldatasource>
</div>
</form>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>