<%@ 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="vb" runat="server">
	Dim Source as SqlConnection
	dim d as string ="SELECT DISTINCT Orders.CusID, Customers.CusLongName FROM Customers INNER JOIN Orders ON Customers.CusID = Orders.CusID"
	sub Page_load(sender as object,e as EventArgs )
		Source = Ctype(Session("NowSTKSQL"),SqlConnection)
		if IsPostBack=false then
			bindNew(true)
		end if
	end sub
	sub bindNew(tag as Boolean) 
		if tag=true then
			Dim DS as DataSet = new DataSet()
			Dim NComm as SqlDataAdapter = new SqlDataAdapter(d, Source)
			NComm.Fill(DS, "Customers")
			Session("NowDSEP")=DS '声明为Session
			Customer.DataSource=DS.Tables("Customers").DefaultView
			Customer.DataBind()
		else
			Dim DS as DataSet = Ctype(Session("NowDSEP"),DataSet)
			Customer.DataSource=DS.Tables("Customers").DefaultView
			Customer.DataBind()
		end if
	end sub
	sub Cus_Creat(Sender as object,e as RepeaterItemEventArgs)
		Dim k as Label = Ctype(e.Item.FindControl("CusID"),Label)
		Dim DS as DataSet = Ctype(Session("NowDSEP"),DataSet)
		if Not Isnothing(k) then
			Dim h as string = "SELECT * FROM Orders Where CusID = '" + k.Text + "'"
			Dim NComm as SqlDataAdapter = new SqlDataAdapter(h, Source)
			NComm.Fill(DS,"Orders")
			Dim De as DataGrid = Ctype(e.Item.FindControl("Detail"),DataGrid)
			De.DataSource=DS.Tables("Orders").DefaultView
			TotalOrders=DS.Tables("Orders").Rows.Count
			Dim h1 as string = "SELECT Sum(BeforeTaxPrice) as BCount,Sum(TotalPrice) as TCount FROM Orders Where CusID = '" + k.Text + "'"
			Dim c as SqlCommand =new SqlCommand(h1,Source)
			c.CommandType = CommandType.Text
			c.Connection.Open()
			Dim re as SqlDataReader=c.ExecuteReader()
			if re.Read()=true then
				BeforeTaxPriceCount=Convert.ToInt32(re.GetInt32(0))
				Dim v as Double =Convert.ToDouble(re.GetDouble(1))
				TotalPriceCount=Convert.ToInt32(v)
			end if
			c.Connection.Close()
			De.DataBind()
			DS.Tables("Orders").Rows.Clear()
		end if		
	end sub
	Property TotalOrders() as Integer
		get
			return Convert.ToInt32(ViewState("TotalOrders"))
		end get 
		set
			ViewState("TotalOrders")=value
		end set
	end property
	Property BeforeTaxPriceCount() as Integer
		get
			return Convert.ToInt32(ViewState("BeforeTaxPriceCount"))
		end get 
		set
			ViewState("BeforeTaxPriceCount")=value
		end set
	end property
	Property TotalPriceCount() as Integer
		get
			return Convert.ToInt32(ViewState("TotalPriceCount"))
		end get
		set
			ViewState("TotalPriceCount")=value
		end set
	end property
	</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>