<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Master/Details Example Using Views: Customer Orders</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server" src="fetchData_sql.cs" />
<script runat="server">
string query;
void Page_Load ( object src, EventArgs e ) {
if ( !IsPostBack ) {
query = "SELECT DISTINCT CustomerId, CompanyName FROM [Orders Summary]";
mySelect.DataSource = fetchReader ( query );
mySelect.DataBind ( );
ordersGrid.SelectedIndex = 0;
getCustomerOrders ( null, null );
getOrderDetails ( null, null );
}
}
public void getCustomerOrders ( object src, EventArgs e ) {
query = "SELECT * FROM [Orders Summary] WHERE CustomerID = '" +
mySelect.Value + "' ORDER BY OrderID";
ordersGrid.DataSource = fetchReader ( query );
ordersGrid.DataBind ( );
ordersGrid.SelectedIndex = 0;
getOrderDetails ( null, null );
}
private void getOrderDetails ( object src, EventArgs e ) {
// filter view with value from ordersGrid's DataKeys collection
if ( ordersGrid.SelectedIndex > -1 ) {
int orderID = int.Parse ( ordersGrid.DataKeys [ ordersGrid.SelectedIndex ].ToString ( ) );
query = "SELECT * FROM [Order Details Extended] WHERE OrderID = " + orderID;
detailsGrid.DataSource = fetchReader ( query );
detailsGrid.DataBind ( );
}
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc -->
<div class="header"><h2>Master/Details Example Using Views: <span class="hilite">Customer Orders</span></h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<center>
<form runat="server">
<p>Select Customer:
<select id="mySelect" datatextfield="CompanyName"
datavaluefield="CustomerId" runat="server" />
<input type=submit value="List Orders" runat="server"
onServerClick="getCustomerOrders" />
<p class="small">Click an invoice ...</p>
<asp:datagrid id="ordersGrid" runat="server"
width=80% cellpadding=5 gridlines="vertical"
font-size="10pt"
alternatingitemstyle-backcolor="whitesmoke"
autogeneratecolumns=false
datakeyfield="OrderID"
onSelectedIndexChanged="getOrderDetails">
<headerstyle backcolor="lightsteelblue"
font-bold height=30
horizontalalign="center" />
<selecteditemstyle backcolor="lightgreen" />
<columns>
<asp:buttoncolumn headertext="Invoice"
datatextfield="OrderID"
commandname="Select" />
<asp:boundcolumn headertext="OrderDate"
datafield="OrderDate"
dataformatstring="{0:d}" />
<asp:boundcolumn headertext="ShippedDate"
datafield="ShippedDate"
dataformatstring="{0:d}" />
<asp:boundcolumn headertext="SalesPerson"
datafield="SalesPerson" />
<asp:boundcolumn headertext="SaleAmount"
datafield="SaleAmount"
dataformatstring="{0:c2}"
itemstyle-horizontalalign="right" />
</columns>
</asp:datagrid>
<p class="small">... to see order details</p>
<asp:datagrid id="detailsGrid" runat="server"
width=80% cellpadding=5 gridlines="vertical"
font-size="10pt" enableviewstate=false
alternatingitemstyle-backcolor="whitesmoke"
autogeneratecolumns=false>
<headerstyle backcolor="lightsteelblue"
font-bold height=30
horizontalalign="center" />
<columns>
<asp:boundcolumn headertext="Invoice"
datafield="OrderId" />
<asp:boundcolumn headertext="Product Name"
datafield="ProductName" />
<asp:boundcolumn headertext="Quantity"
datafield="Quantity"
itemstyle-horizontalalign="right" />
<asp:boundcolumn headertext="Unit Price"
datafield="UnitPrice"
dataformatstring="{0:c}"
itemstyle-horizontalalign="right" />
<asp:boundcolumn headertext="Discount"
datafield="Discount"
dataformatstring="{0:n}"
itemstyle-horizontalalign="right" />
<asp:boundcolumn headertext="Net Amount"
datafield="Net Amount"
dataformatstring="{0:c}"
itemstyle-horizontalalign="right" />
</columns>
</asp:datagrid>
</form>
</center>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>