<%@ Register TagPrefix="Title" TagName="TitleShow" Src="/aspnetdata/Title.ascx" %>
<%@Page Debug=True %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web" %>
<html>
<head>
	<script language="C#" runat="server">
	SqlConnection Source;
	string d="SELECT DISTINCT Orders.CusID, Customers.CusLongName FROM Customers INNER JOIN Orders ON Customers.CusID = Orders.CusID";
	void Page_load(object sender,EventArgs e){
		Source = (SqlConnection)Session["NowSTKSQL"];
		if(IsPostBack==false){
			bindNew(true);
			}
	}
	void bindNew(Boolean tag){//结合至DataGrid
		if(tag==true){
			DataSet DS = new DataSet();
			SqlDataAdapter NComm = new SqlDataAdapter(d, Source);
			NComm.Fill(DS, "Customers");
			Session["NowDSEP"]=DS;//声明为Session
			Customer.DataSource=DS.Tables["Customers"].DefaultView;
			Customer.DataBind();
		}
		else{
			DataSet DS=(DataSet)Session["NowDSEP"];
			Customer.DataSource=DS.Tables["Customers"].DefaultView;
			Customer.DataBind();
		}
	}
	void Cus_Creat(object sender,RepeaterItemEventArgs e){
		Label k = (Label)e.Item.FindControl("CusID");
		DataSet DS = (DataSet)Session["NowDSEP"];
		if(k!=null){
			string h = "SELECT * FROM Orders Where CusID = '" + k.Text + "'";
			SqlDataAdapter NComm = new SqlDataAdapter(h, Source);
			NComm.Fill(DS,"Orders");
			DataGrid De=(DataGrid)e.Item.FindControl("Detail");
			De.DataSource=DS.Tables["Orders"].DefaultView;
			TotalOrders=DS.Tables["Orders"].Rows.Count;
			string h1 = "SELECT Sum(BeforeTaxPrice) as BCount,Sum(TotalPrice) as TCount FROM Orders Where CusID = '" + k.Text + "'";
			SqlCommand c=new SqlCommand(h1,Source);
			c.CommandType = CommandType.Text;
			c.Connection.Open();
			SqlDataReader re=c.ExecuteReader();
			if(re.Read()==true){
				BeforeTaxPriceCount=Convert.ToInt32(re.GetInt32(0));
				Double v=Convert.ToDouble(re.GetDouble(1));
				TotalPriceCount=Convert.ToInt32(v);
			}
			c.Connection.Close();
			De.DataBind();
			DS.Tables["Orders"].Rows.Clear();
		}		
	}
	int TotalOrders{
		get{
			return Convert.ToInt32(ViewState["TotalOrders"]);
		} 
		set{
			ViewState["TotalOrders"]=value;}
	}
	int BeforeTaxPriceCount{
		get{
			return Convert.ToInt32(ViewState["BeforeTaxPriceCount"]);
		} 
		set{
			ViewState["BeforeTaxPriceCount"]=value;}
	}
	int TotalPriceCount{
		get{
			return Convert.ToInt32(ViewState["TotalPriceCount"]);
		} 
		set{
			ViewState["TotalPriceCount"]=value;}
	}
	</script>
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form runat="server">
<asp:Repeater ID="Customer" Runat="server"
OnItemDataBound="Cus_Creat" >
            <SeparatorTemplate>
              ---------------------------------------------------------------------<br>
             </SeparatorTemplate>
              <HeaderTemplate>
                所有已来往之客户：<br>
              </HeaderTemplate>
              <ItemTemplate>
客户名称：<asp:Label Text='<%# DataBinder.Eval(Container.DataItem,"CusLongName")%>' id="Name" Runat="server" /><br>
客户编号：<asp:Label Text='<%# DataBinder.Eval(Container.DataItem,"CusID")  %>' id="CusID" Runat="server" />
<blockquote>
<asp:DataGrid ID="Detail" Runat="server" 
        BorderColor="black"
        BorderWidth="1"
        GridLines="Both"
        CellPadding="3"
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="8pt"
	ShowFooter="true"
        HeaderStyle-BackColor="LightYellow"
        AutoGenerateColumns="False">
      <Columns>
<asp:TemplateColumn HeaderText="订单编号" HeaderSTyle-HorizontalAlign="center">
	<itemTemplate>
		<asp:label runat="server" text='<%# DataBinder.Eval(Container.DataItem,"OrderID")%>' />
	</itemTemplate>
         <FooterTemplate>
		共<asp:Label ID="NowTotal" Text="<%# TotalOrders %>" Runat="server" />个
         </FooterTemplate>
</asp:TemplateColumn>
        <asp:BoundColumn HeaderText="下单日期" ReadOnly="True" DataField="OrderDate"/>
<asp:TemplateColumn HeaderText="未含税价" HeaderSTyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="right" FooterStyle-HorizontalAlign="right">
	<itemTemplate>
		<asp:label runat="server" text='<%# DataBinder.Eval(Container.DataItem,"BeforeTaxPrice")%>' />
	</itemTemplate>
         <FooterTemplate>
		<asp:Label ID="NowBeforeTaxTotal" Text="<%# BeforeTaxPriceCount %>" Runat="server" />
         </FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="含税总价" HeaderSTyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="right" FooterStyle-HorizontalAlign="right">
	<itemTemplate>
		<asp:label runat="server" text='<%# DataBinder.Eval(Container.DataItem,"TotalPrice","{0:n}")%>' />
	</itemTemplate>
         <FooterTemplate>
		<asp:Label ID="NowTaxTotal" Text='<%# TotalPriceCount.ToString("#,###") %>' Runat="server" />
         </FooterTemplate>
</asp:TemplateColumn>
      </Columns>
</asp:Datagrid>
</blockquote>
      </ItemTemplate>
</asp:Repeater>
</form>