<%@Page Debug=True %>
<%@ Register TagPrefix="Title" TagName="TitleShow" Src="/aspnetdata/Title.ascx" %>
<%@ Register TagPrefix="Drop" TagName="Dropdown" Src="Drop.ascx" %>
<%@ Register TagPrefix="Cal" TagName="Calendar" Src="cal.ascx" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web" %>
<html>
<head>
	<script language="C#" runat="server">
	SqlConnection Source;
	string k="";
	void Page_load(object sender, EventArgs e){
		k = "SELECT Orders.*, Customers.CusName, Employees.Cname";
		k += " FROM (Orders INNER JOIN Customers ON Orders.CusID = ";
		k += "Customers.CusID) INNER JOIN Employees ON Orders.Employ = Employees.EmID Order By OrderID Desc";
		Source = (SqlConnection)Session["NowSTKSQL"];
		if(IsPostBack==false){
			bindNew(true);
		}
	}
	void bindNew(Boolean tag){
		ErrorSave.Text="";
		if(tag == true){
			DataSet DS = new DataSet();
			SqlDataAdapter NComm = new SqlDataAdapter(k, Source);
			NComm.Fill(DS, "ShowOrders");
			Session["NowDSOR"]=DS;//声明为Session
			Orders.DataSource=DS.Tables["ShowOrders"].DefaultView;
			Orders.DataBind();
		}
		else{
			DataSet DS = (DataSet)Session["NowDSOR"];
			Orders.DataSource=DS.Tables["ShowOrders"].DefaultView;
			Orders.DataBind();
		}
	}
	void Order_Edit(object sender,DataGridCommandEventArgs e){
		Orders.EditItemIndex = e.Item.ItemIndex;
		Orders.SelectedIndex=e.Item.ItemIndex;
		bindNew(false);
		Label p=(Label)Orders.Items[e.Item.ItemIndex].Cells[2].FindControl("CusID1");
		ASP.Drop_ascx x;
		x=(ASP.Drop_ascx)Orders.Items[Orders.EditItemIndex].FindControl("CusID");
		x.Sname="Select * from Customers";
		x.TextField="CusName";
		x.ValueField="CusID";
		x.CompareValue=p.Text;
		x.CompareField="CusID";
		x.SessionName="NowSTKSQL";
		x.Show();
	}
	void Order_Cancel(object sender,DataGridCommandEventArgs e){
		Orders.EditItemIndex = -1;// '取消
		Orders.SelectedIndex = -1;
		bindNew(false);
	}
	void Order_Update(object sender, DataGridCommandEventArgs e){
		DataSet NowDS = (DataSet)Session["NowDSOR"];
		SqlDataAdapter OrderCom = new SqlDataAdapter("Select * from Orders where OrderID Like '" + Orders.DataKeys[Orders.SelectedIndex] + "'", Source);
		OrderCom.Fill(NowDS,"Orders");
		try{//尝试保存
			ASP.Drop_ascx x;
			x=(ASP.Drop_ascx)Orders.Items[Orders.EditItemIndex].Cells[2].FindControl("CusID");
			NowDS.Tables["Orders"].Rows[0]["CusID"]=x.GetNowSelect;
			ASP.Cal_ascx a;
			a=(ASP.Cal_ascx)Orders.Items[Orders.EditItemIndex].Cells[3].FindControl("OrderDate");
			NowDS.Tables["Orders"].Rows[0]["OrderDate"]=a.NowDate;
			SqlCommandBuilder custCB = new SqlCommandBuilder(OrderCom);
			OrderCom.Update(NowDS,"Orders");
			Orders.EditItemIndex = -1;
			bindNew(true);
		}
		catch{
			ErrorSave.Text = "发生错误，无法保存";
		}
	}
	</script>
<link rel=stylesheet type="text/css" href="Style.css">
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form runat="server">
查看 <asp:hyperlink id="h1" NavigateUrl="/aspnetdata/shows.aspx?cate=c&file=ch12/12-2/cs/Drop.ascx" Text="Drop.ascx" runat="server" target="_blank"/>、
<asp:hyperlink id="h2" NavigateUrl="/aspnetdata/shows.aspx?cate=c&file=ch12/12-2/cs/Cal.ascx" Text="Cal.ascx" runat="server" target="_blank"/><br>
      <ASP:DataGrid id="Orders" runat="server"
        BorderColor="black"
        BorderWidth="1"
        GridLines="Both"
        CellPadding="3"
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="10pt"
        HeaderStyle-BackColor="GreenYellow"
AlternatingItemStyle-BackColor="LightYellow"
SelectedItemStyle-BackColor="LightCyan"
SelectedItemStyle-Font-Bold="true"
DataKeyField="OrderID"
AutoGenerateColumns="false"
      OnEditCommand="Order_Edit"
      OnCancelCommand="Order_Cancel"
      OnUpdateCommand="Order_Update"
>
      <Columns>
        <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Save" ItemStyle-Wrap="false"/>
        <asp:BoundColumn HeaderText="订单编号" ReadOnly="True" DataField="OrderID" ItemStyle-Wrap="false"/>
<asp:TemplateColumn HeaderText="客户名称" >
          <ItemTemplate>
<asp:Label runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "CusName") %> id="CusName"/>
          </ItemTemplate>
          <EditItemTemplate>
<asp:Label runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "CusID") %> id="CusID1"/>
            <Drop:Dropdown runat="server" id="CusID" />
          </EditItemTemplate>
        </asp:TemplateColumn>
<asp:TemplateColumn HeaderText="下单日期" >
          <ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "OrderDate","{0:yyyy/MM/dd}") %>' id="OrderDatel"/>
          </ItemTemplate>
          <EditItemTemplate>
<Cal:Calendar ID="OrderDate" NowDate=<%# DataBinder.Eval(Container.DataItem, "OrderDate","{0:yyyy/MM/dd}") %> runat="server" />
          </EditItemTemplate>
        </asp:TemplateColumn>
<asp:TemplateColumn HeaderText="未含税价" >
          <ItemTemplate>
<asp:Label runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "BeforeTaxPrice") %> id="BeforeTaxPrice1"/>
          </ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="含税否" >
          <ItemTemplate>
<asp:CheckBox runat="server" Enabled="False" Checked=<%# DataBinder.Eval(Container.DataItem, "Tax") %> id="Taxl"/>
          </ItemTemplate>
        </asp:TemplateColumn>
<asp:TemplateColumn HeaderText="税金" >
          <ItemTemplate>
<asp:Label runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "TaxPrice") %> id="TaxPrice"/>
          </ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="含税价" >
          <ItemTemplate>
<asp:Label runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "TotalPrice") %> id="TotalPrice"/>
          </ItemTemplate>
</asp:TemplateColumn>
      </Columns>
</ASP:DataGrid>

<asp:Label ID="ErrorSave" Tooltip="ErrorFile" Runat="server" />
<asp:label id="nn" Runat="server" />
</form>
</body>