<%@ 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" %>
<%@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 Orders.OrderID, Orders.CusID, Customers.CusName,Orders.OrderDate, Orders.BeforeTaxPrice, Orders.TotalPrice, Orders.TaxPrice, Orders.Tax FROM Orders INNER JOIN Customers ON Orders.CusID = Customers.CusID"
	Dim NowRecord as integer
	sub Page_load(sender as object,e as EventArgs)
		Source = Ctype(Session("NowSTKSQL"),SqlConnection)
		if IsPostBack=false then
			NowRecord = 0 '取得第一条记录
			Status.Text="查看记录"
			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, "Orders")
			NComm.Fill(DS, "OrdersN")
			DS.Tables("OrdersN").Rows.Clear()
			Session("NowDO")=DS'声明为Session
			ShowRecord() '执行显示记录
		else
			Dim DS as DataSet=Ctype(Session("NowDO"),DataSet)
			Orders.DataSource=DS.Tables("OrdersN").DefaultView
			Orders.DataBind()
		end if
	end sub
	sub ShowRecord()
		Dim NowDS as DataSet = Ctype(Session("NowDO"),DataSet)
		NowDS.Tables("OrdersN").Rows.Clear()
		if NowRecord >= 0 then '非添加时
			Dim NRow as DataRow = NowDS.Tables("Orders").Rows(NowRecord)
			NowDS.Tables("OrdersN").ImportRow(NRow)
			Orders.DataSource=NowDS.Tables("OrdersN").DefaultView
			Orders.DataBind()
			CurrentRecord.Text=(NowRecord+1).ToString()
			AllRecords.Text=NowDS.Tables("Orders").Rows.Count.ToString()
			try
			Dim x as ASP.Drop_ascx
			x=Ctype(Orders.Items(0).FindControl("CusID"),ASP.Drop_ascx)
			x.Sname="Select * from Customers"
			x.TextField="CusName"
			x.ValueField="CusID"
			x.CompareValue=NowDS.Tables("OrdersN").Rows(0)("CusID").ToString()
			x.SessionName="NowSTKSQL"
			x.CompareField="CusID"
			x.Show()
			catch
			end try
		else
			Dim DS as DataSet = Ctype(Session("NowDO"),DataSet)
			NowDS.Tables("OrdersN").Rows.Clear()'清除记录
			Dim NwRow as DataRow =DS.Tables("OrdersN").NewRow()'添加
			NwRow("Tax")=0'为各栏指定预设值
			NwRow("TaxPrice")=0
			NwRow("BeforeTaxPrice")=0
			NwRow("TotalPrice")=0
			DS.Tables("OrdersN").Rows.Add(NwRow)
			Orders.DataSource=NowDS.Tables("OrdersN").DefaultView
			Orders.EditItemIndex=0
			Orders.DataBind()
			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=""
			x.CompareField="CusID"
			x.SessionName="NowSTKSQL"
			x.Show()
		end if
	end sub
	sub ChangeRecord(sender as object,e as CommandEventArgs)
		if e.CommandArgument = "Next" then'下条
			if CurrentRecord.Text=AllRecords.Text then
				NowRecord = Convert.ToInt32(AllRecords.Text)-1
			else
				CurrentRecord.Text=(Convert.ToInt32(CurrentRecord.Text)+1).ToString()
				NowRecord = Convert.ToInt32(CurrentRecord.Text)-1
			end if
		elseif e.CommandArgument = "First" then'首条
			CurrentRecord.Text="1"
			NowRecord = 0
		elseif e.CommandArgument = "Last" then'末条
			CurrentRecord.Text=(Convert.ToInt32(AllRecords.Text)).ToString()
			NowRecord = Convert.ToInt32(AllRecords.Text) - 1
		elseif e.CommandArgument = "Prev" then'上条
			if Convert.ToInt32(CurrentRecord.Text) > 1 then
				CurrentRecord.Text=(Convert.ToInt32(CurrentRecord.Text) - 1).ToString()
				NowRecord = Convert.ToInt32(CurrentRecord.Text)-1
			else
				NowRecord = 1
			end if
		end if
		ShowRecord()
	end sub
	sub GotoRecord(sender as object,e as EventArgs)
		try
			dim k as integer = Convert.ToInt32(ToRecord.Text)'取得输入之数字
			if k>Convert.ToInt32(AllRecords.Text) then'若输入大于总条数
				k = Convert.ToInt32(AllRecords.Text)
			end if
			if k<0 then k = 1
			NowRecord=k-1
			ToRecord.Text=k.ToString()
			CurrentRecord.Text=k.ToString()
			ShowRecord()
		catch
			NowRecord=0
			ToRecord.Text="1"
			CurrentRecord.Text="1"
			ShowRecord()
		end try
	end sub
	sub Orders_Edit(sender as object,e as DataListCommandEventArgs)
		Orders.EditItemIndex = e.Item.ItemIndex
		bindNew(false)
		Dim f as TextBox =Ctype(Orders.Items(0).FindControl("OrderID"),TextBox)
		f.Visible=false
		Dim DS as DataSet = Ctype(Session("NowDO"),DataSet)
		Dim NRow as DataRow = DS.Tables("OrdersN").Rows(0)
		dim h as string =NRow("CusID").ToString()
		Dim k as Control=Ctype(Orders,Control)
		AddOrder.Visible=false
		Status.Text="编辑记录"
		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=NRow("CusID").ToString()
		x.SessionName="NowSTKSQL"
		x.CompareField="CusID"
		x.Show()'显示Drop.ascx
	end sub
	sub Orders_Update(sender as object,e as DataListCommandEventArgs )
		Dim DS as DataSet = Ctype(Session("NowDO"),DataSet)
		Dim w as string =""
		Dim BeforeTaxPrice as TextBox = Ctype(e.Item.FindControl("BeforeTaxPrice"),TextBox)
		Dim TaxPrice as TextBox = Ctype(e.Item.FindControl("TaxPrice"),TextBox)
		Dim TotalPrice as TextBox = Ctype(e.Item.FindControl("TotalPrice"),TextBox)
		Dim k as Double =Convert.ToDouble(TaxPrice.Text) + Convert.ToDouble(BeforeTaxPrice.Text)'取得含税价
		Dim x as ASP.Drop_ascx
		x=Ctype(Orders.Items(Orders.EditItemIndex).FindControl("CusID"),ASP.Drop_ascx)
		Dim a as ASP.Cal_ascx
		a=Ctype(Orders.Items(Orders.EditItemIndex).FindControl("OrderDate"),ASP.Cal_ascx)
		dim y as integer =0
		if TaxPrice.Text<>"0" then'若不等于0则y等于1
			y=1'y是含税否
		end if
		if Status.Text="编辑记录" then
			Dim NowID as Label = Ctype(e.Item.FindControl("OrderIDV"),Label)
			w = "Update Orders set"
			w += " OrderDate ='" & a.NowDate & "', BeforeTaxPrice =" & Teststring(BeforeTaxPrice.Text)
			w += ", TaxPrice =" & Teststring(TaxPrice.Text) & ", TotalPrice =" & k
			w += ", Tax = " + y & ",CusID ='" & x.GetNowSelect & "' where OrderID ='" & NowID.Text & "'"
		else
			Dim NowId as TextBox = Ctype(e.Item.FindControl("OrderID"),TextBox)
			w = "Insert Into Orders (OrderID,OrderDate,BeforeTaxPrice,Tax,TaxPrice,TotalPrice,CusID) Values ( '"
			w += Nowid.Text & "','" & a.NowDate & "'," & Teststring(BeforeTaxPrice.Text) & "," + y & "," & Teststring(TaxPrice.Text)
			w += "," & Teststring(TotalPrice.Text) & ",'" & x.GetNowSelect & "')"
		end if
		try
			Dim UpOrder as SqlCommand =new SqlCommand(w,Source)
			UpOrder.Connection.Open()
			UpOrder.ExecuteNonQuery()'执行保存
			UpOrder.Connection.Close()
			Orders.EditItemIndex = -1
			NowRecord=Convert.ToInt32(CurrentRecord.Text)-1
			Orders.EditItemIndex = -1
			NowRecord=Convert.ToInt32(CurrentRecord.Text)-1
			bindNew(true)
			AddOrder.Visible=true
			Status.Text="查看记录"
		catch
			errMsg.Text="以" & w & "添加失败"
		end try
	end sub
	sub Orders_Cancel(sender as object,e as DataListCommandEventArgs )
		if Status.Text="添加记录" then
			Status.Text="查看记录"
			dim x as integer =Convert.ToInt32(AllRecords.Text)
			NowRecord=x-2
			AllRecords.Text=(x-1).ToString()
			CurrentRecord.Text=(x-1).ToString()
			ShowRecord()
		end if
		Orders.EditItemIndex = -1
		AddOrder.Visible=true
		bindNew(false)
	end sub
	sub AddOrder_Click(sender as object,e as EventArgs)
		NowRecord=-1
		ShowRecord()
		Status.Text="添加记录"
		dim f as Label =Ctype(Orders.Items(0).FindControl("OrderIDV"),Label)
		f.Visible=false'隐藏
		Dim x as integer =Convert.ToInt32(AllRecords.Text)+1
		AllRecords.Text=x.ToString()'总条数加1
		CurrentRecord.Text=x.ToString()
		AddOrder.Visible=false
	end sub
	Function Teststring(h as string) as Double
		if h="" then
			return 0
		else
			return Convert.ToDouble(h)
		end if
	end function
	</script>
<script language="javascript">
<!--
function CalCu(){
var b=0;
var x=0;
var c=0;
b=Order.Orders__ctl1_BeforeTaxPrice.value-c;
if(Order.TaxClient.checked=true){
	x=b*0.05;
	Order.Orders__ctl1_TaxPrice.value=x;
	Order.Orders__ctl1_TotalPrice.value=b + x;
}else{
	Order.Orders__ctl1_TaxPrice.value=0;
	Order.Orders__ctl1_TotalPrice.value=b;
}
}
' -->
</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" id="Order">
查看 <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:LinkButton CausesValidation="false" ID="First" Tooltip="First" OnCommand="ChangeRecord" Runat="server" Text="首条" CommandArgument="First" cssclass="Tab_08" />
<asp:LinkButton CausesValidation="false" ID="Prev" Tooltip="Prev" OnCommand="ChangeRecord" Runat="server" Text="上条" CommandArgument="Prev"  cssclass="Tab_08"/>
<asp:LinkButton CausesValidation="false" ID="Next" Tooltip="Next" OnCommand="ChangeRecord" Runat="server" Text="下条" CommandArgument="Next"  cssclass="Tab_08"/>
<asp:LinkButton CausesValidation="false" ID="Last" Tooltip="Last" OnCommand="ChangeRecord" Runat="server" Text="末条" CommandArgument="Last"  cssclass="Tab_08"/>
至第<asp:TextBox Columns="3" AutoPostBack="true" ID="ToRecord" OnTextChanged="GotoRecord" Runat="server" />条
<p><ASP:LINKBUTTON ID="AddOrder" runat="server" onclick="AddOrder_Click" Text="Add" />　第<asp:Label ID="CurrentRecord" Tooltip="CurrentRecord" Runat="server" />条，共<asp:Label ID="AllRecords" Tooltip="AllRecords" Runat="server" />条　　
<font color="#FF0000"><b>目前状态：<asp:label id="Status" Tooltip="status" runat="server" /></b></font>
<font color="#FF0000"><b><asp:label id="errMsg" runat="server" /></b></font><br>
<asp:DataList id="Orders" runat="server"
	BorderColor="black"	BorderWidth="1"
	GridLines="Both"	CellPadding="3"
	CellSpacing="0" 	Font-Name="Verdana"
	Font-Size="11pt" 	RepeatColumns="1"
	Width="500px"		HeaderStyle-BackColor="#C0FFFF"
	AlternatingItemStyle-BackColor="Gainsboro"
	EditItemStyle-BackColor="#FFFFC0"
itemStyle-width="450"
	OnEditCommand="Orders_Edit"
	OnUpdateCommand="Orders_Update"
	OnCancelCommand="Orders_Cancel"
            >
	<HeaderTemplate>
                订单基本资料
	</HeaderTemplate>
	<ItemTemplate>
		<asp:LinkButton id="button1" runat="server" Text="Edit" CommandName="edit" /><br>
<table><tr><td width="55%">
		订单编号：<asp:Label Text='<%# DataBinder.Eval(Container.DataItem,"OrderID") %>' ID="OrderIDL" runat="server" /><br>
</td><td>
		下单日期：<%# DataBinder.Eval(Container.DataItem,"OrderDate","{0:yyyy/MM/dd}")  %><br>
</td></tr><tr><td>
		客户：<%# DataBinder.Eval(Container.DataItem,"CusName")  %>
<asp:label id="CusIDV" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CusID") %>' visible="false"/>
</td></tr><tr><td>
		未含税价：<asp:Label Text='<%# DataBinder.Eval(Container.DataItem,"BeforeTaxPrice","{0:#,###}") %>' id="BeforeTaxPriceV" runat="server" cssclass="Tab_07"/>
		含税否：<asp:checkbox Checked='<%# DataBinder.Eval(Container.DataItem,"Tax") %>' runat="server" Enabled="false"/>
</td><td>
		税金：<asp:Label Text='<%# DataBinder.Eval(Container.DataItem,"TaxPrice","{0:#,###}") %>' id="TaxPrice" Runat="server" cssclass="Tab_07"/>
		含税价：<asp:Label Text='<%# DataBinder.Eval(Container.DataItem,"TotalPrice","{0:#,###}") %>' id="TotalPrice" Runat="server" cssclass="Tab_07"/>
</td></tr></table>
	</ItemTemplate>
	<EditItemTemplate>
		<asp:LinkButton id="button2" runat="server" Text="Save" CommandName="Update" />
		<asp:LinkButton id="button3" runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="false" /><br>
<table><tr><td width="50%">
		订单编号：<asp:Label id="OrderIDV" Text='<%# DataBinder.Eval(Container.DataItem,"OrderID") %>' runat="server" />
			<asp:TextBox Text='<%# DataBinder.Eval(Container.DataItem,"OrderID") %>' ID="OrderID" runat="server" />
<br>
</td><td>
		下单日期：
<Cal:Calendar ID="OrderDate" NowDate=<%# DataBinder.Eval(Container.DataItem, "OrderDate","{0:yyyy/MM/dd}") %> runat="server" /><br>
</td></tr><tr><td>
		客户：<Drop:Dropdown id="CusID" Runat="server" />
</td></tr><tr><td>
		未含税价：<asp:TextBox Enabled="false" ID="BeforeTaxPrice" Text='<%# DataBinder.Eval(Container.DataItem,"BeforeTaxPrice") %>' Runat="server" width="50"/>
含税否：<input id="TaxClient" type="checkbox" Checked='<%# DataBinder.Eval(Container.DataItem,"Tax") %>' onclick="javascript:CalCu()">
</td><td>
		税金：<asp:TextBox text='<%# DataBinder.Eval(Container.DataItem,"TaxPrice","{0:#,###}") %>' ID="TaxPrice" Runat="server" width="35"/>
		含税价：<asp:TextBox Enabled="false" Text='<%# DataBinder.Eval(Container.DataItem,"TotalPrice") %>' ID="TotalPrice" Runat="server" width="55"/>
</td></tr></table>
	</EditItemTemplate>
</asp:DataList>

</form>