<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Dual Nested Templated Controls Example</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server" src="fetchData_sql.cs" />
<script language="C#" runat="server">
int prev;
void Page_Load ( Object src, EventArgs e ) {
if ( !IsPostBack ) {
// connect to data source
SqlConnection myConn = new SqlConnection (
getConnection ( "aspnet" ) );
string query = "SELECT CustomerID, CompanyName FROM Customers";
SqlDataAdapter da = new SqlDataAdapter ( query, myConn );
DataSet ds = new DataSet ( );
da.Fill ( ds );
Customers.DataSource = ds.Tables [ 0 ].DefaultView;
Customers.DataBind ( );
}
}
void getOrdersSource ( Object src, DataListItemEventArgs e ) {
if ( e.Item.ItemType == ListItemType.SelectedItem ) {
DataList Orders = ( DataList ) e.Item.FindControl ( "Orders" );
Orders.DataSource = ( ( DataRowView ) e.Item.DataItem ).CreateChildView ( "custID" );
Orders.DataBind ( );
}
}
void getOrderDetailsSource ( Object src, DataListItemEventArgs e ) {
if ( e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem ) {
DataGrid OrderDetails = ( DataGrid ) e.Item.FindControl ( "OrderDetails" );
OrderDetails.DataSource = ( ( DataRowView ) e.Item.DataItem ).CreateChildView ( "orderID" );
OrderDetails.DataBind ( );
}
}
void showOrders ( Object src, DataListCommandEventArgs e ) {
Customers.SelectedIndex = e.Item.ItemIndex;
fetchData ( );
}
void fetchData ( ) {
// connect to data source
SqlConnection myConn = new SqlConnection (
getConnection ( "aspnet" ) );
string query = "SELECT CustomerID, CompanyName FROM Customers; select * FROM Orders; Select* FROM [Order Details]";
SqlDataAdapter da = new SqlDataAdapter ( query, myConn );
DataSet ds = new DataSet ( );
da.Fill ( ds );
// define a relationship between Customers and Orders tables
ds.Relations.Add ( "custId",
ds.Tables [ 0 ].Columns [ "CustomerID" ] ,
ds.Tables [ 1 ].Columns [ "CustomerID" ] );
// define a relationship between orders and OrderDetails tables
ds.Relations.Add ( "orderID",
ds.Tables [ 1 ].Columns [ "OrderID" ] ,
ds.Tables [ 2 ].Columns [ "OrderID" ] );
Customers.DataSource = ds.Tables [ 0 ].DefaultView;
Customers.DataBind ( );
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>Dual Nested Templated Controls Example</h2></div>
<hr size=1 width=92%>
<form runat="server">
<div align="center">
<asp:datalist id="Customers" runat="server"
width=90% cellpadding=5 repeatcolumns=2
backcolor="ghostwhite"
onItemDataBound="getOrdersSource"
onItemCommand="showOrders">
<headerstyle height="25pt" font-bold
backcolor="darkslategray" forecolor="khaki" />
<itemstyle verticalalign="top" />
<selecteditemstyle backcolor="lightsteelblue" width=50% />
<headertemplate>
NorthWind Traders : Orders By Customers
</headertemplate>
<itemtemplate>
<asp:linkbutton runat="server"
text='<%# Eval ( "CompanyName" ) %>' />
</itemtemplate>
<selecteditemtemplate>
<h5 style="background:maroon; color:lightyellow; padding:3">
<%# Eval ( "CompanyName" ) %>
</h5>
<center>
<asp:datalist id="Orders" runat="server"
width=98% cellpadding=5
repeatcolumns=2 font-size="9pt"
itemstyle-verticalalign="top"
onItemDataBound="getOrderDetailsSource">
<itemtemplate>
<b>Inv. # <%# Eval ( "OrderID" ) %></b>
<br>
<%# Eval (
"ShippedDate", "{0:D}" ) %>
<center>
<asp:datagrid id="OrderDetails" runat="server"
width=98% cellpadding=3 font-size="9pt"
gridlines="horizontal" autogeneratecolumns=false>
<headerstyle height="18pt" font-bold
backcolor="navy" forecolor="lime"
horizontalalign="center" />
<itemstyle backcolor="whitesmoke" verticalalign="top" />
<columns>
<asp:boundcolumn headertext="Stock"
datafield="productid" />
<asp:boundcolumn headertext="Qty"
datafield="quantity" />
<asp:boundcolumn headertext="Unit Price"
datafield="unitprice"
dataformatstring="{0:c}"
itemstyle-horizontalalign="right"/>
<asp:boundcolumn headertext="Discount"
datafield="discount"
itemstyle-horizontalalign="right"/>
</columns>
</asp:datagrid>
</center>
</itemtemplate>
</asp:datalist>
</center>
</selecteditemtemplate>
</asp:datalist>
</div>
</form>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>