<%@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="VB" runat="server">
	Dim Source as SqlConnection
	Dim k as string
	sub Page_load(sender as object, e as EventArgs)
		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 = Ctype(Session("NowSTKSQL"),SqlConnection)
		if IsPostBack=false then
			bindNew(true)
		end if
	end sub
	sub bindNew(tag as Boolean)
		ErrorSave.Text=""
		if tag = true then
			Dim DS as DataSet = new DataSet()
			Dim NComm as SqlDataAdapter = new SqlDataAdapter(k, Source)
			NComm.Fill(DS, "ShowOrders")
			Session("NowDSOR")=DS'声明为Session
			Orders.DataSource=DS.Tables("ShowOrders").DefaultView
			Orders.DataBind()
		else
			Dim DS as DataSet = Ctype(Session("NowDSOR"),DataSet)
			Orders.DataSource=DS.Tables("ShowOrders").DefaultView
			Orders.DataBind()
		end if
	end sub
	sub Order_Edit(sender as object,e as DataGridCommandEventArgs)
		Orders.EditItemIndex = e.Item.ItemIndex
		Orders.SelectedIndex=e.Item.ItemIndex
		bindNew(false)
		Dim p as Label=Ctype(Orders.Items(e.Item.ItemIndex).Cells(2).FindControl("CusID1"),Label)
		Dim x as ASP.Drop_ascx
		x=Ctype(Orders.Items(Orders.EditItemIndex).FindControl("CusID"),ASP.Drop_ascx)
		x.Sname="Select * from Customers"
		x.TextField="CusName"
		x.ValueField="CusID"
		x.CompareValue=p.Text
		x.CompareField="CusID"
		x.SessionName="NowSTKSQL"
		x.Show()
	end sub
	sub Order_Cancel(sender as object,e as DataGridCommandEventArgs)
		Orders.EditItemIndex = -1' '取消
		Orders.SelectedIndex = -1
		bindNew(false)
	end sub
	sub Order_Update(sender as object,e as DataGridCommandEventArgs)
		Dim NowDS as DataSet = Ctype(Session("NowDSOR"),DataSet)
		Dim OrderCom as SqlDataAdapter = new SqlDataAdapter("Select * from Orders where OrderID Like '" & Orders.DataKeys(Orders.SelectedIndex) & "'", Source)
		OrderCom.Fill(NowDS,"Orders")
		try'尝试保存
			dim x as ASP.Drop_ascx
			x=Ctype(Orders.Items(Orders.EditItemIndex).Cells(2).FindControl("CusID"),ASP.Drop_ascx)
			NowDS.Tables("Orders").Rows(0)("CusID")=x.GetNowSelect
			Dim a as ASP.Cal_ascx
			a=Ctype(Orders.Items(Orders.EditItemIndex).Cells(3).FindControl("OrderDate"),ASP.Cal_ascx)
			NowDS.Tables("Orders").Rows(0)("OrderDate")=a.NowDate
			Dim custCB as SqlCommandBuilder = new SqlCommandBuilder(OrderCom)
			OrderCom.Update(NowDS,"Orders")
			Orders.EditItemIndex = -1
			bindNew(true)
		catch
			ErrorSave.Text = "发生错误，无法保存"
		end try
	end sub
	</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/vb/Drop.ascx" Text="Drop.ascx" runat="server" target="_blank"/>、
<asp:hyperlink id="h2" NavigateUrl="/aspnetdata/shows.aspx?cate=c&file=ch12/12-2/vb/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>