<%@ 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="C#" runat="server">
	SqlConnection Source;
	string d="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";
	int NowRecord;//目前条数
	void Page_load(object sender,EventArgs e){
		Source = (SqlConnection)Session["NowSTKSQL"];
		if(IsPostBack==false){
			NowRecord = 0; //取得第一条记录
			Status.Text="查看记录";
			bindNew(true);
			}
	}
	void bindNew(Boolean tag){
		if(tag==true){
			DataSet DS = new DataSet();
			SqlDataAdapter NComm = new SqlDataAdapter(d, Source);
			NComm.Fill(DS, "Orders");
			NComm.Fill(DS, "OrdersN");
			DS.Tables["OrdersN"].Rows.Clear();
			Session["NowDO"]=DS;//声明为Session
			ShowRecord(); //执行显示记录
		}
		else{
			DataSet DS=(DataSet)Session["NowDO"];
			Orders.DataSource=DS.Tables["OrdersN"].DefaultView;
			Orders.DataBind();
		}
	}
	void ShowRecord(){
		DataSet NowDS = (DataSet)Session["NowDO"];
		NowDS.Tables["OrdersN"].Rows.Clear();
		if(NowRecord >= 0){ //非添加时
			DataRow NRow = 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{
			ASP.Drop_ascx x;
			x=(ASP.Drop_ascx)Orders.Items[0].FindControl("CusID");
			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{}
		}
		else{
			DataSet DS = (DataSet)Session["NowDO"];
			NowDS.Tables["OrdersN"].Rows.Clear();//清除记录
			DataRow NwRow=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();
			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="";
			x.CompareField="CusID";
			x.SessionName="NowSTKSQL";
			x.Show();
		}
	}
	void ChangeRecord(object sender, CommandEventArgs e){
		if(e.CommandArgument == "Next"){//下条
			if(CurrentRecord.Text==AllRecords.Text){
				NowRecord = Convert.ToInt32(AllRecords.Text)-1;
			}
			else{
				CurrentRecord.Text=(Convert.ToInt32(CurrentRecord.Text)+1).ToString();
				NowRecord = Convert.ToInt32(CurrentRecord.Text)-1;
			}
		}
		else if(e.CommandArgument == "First"){//首条
			CurrentRecord.Text="1";
			NowRecord = 0;
		}
		else if(e.CommandArgument == "Last"){//末条
			CurrentRecord.Text=(Convert.ToInt32(AllRecords.Text)).ToString();
			NowRecord = Convert.ToInt32(AllRecords.Text) - 1;
		}
		else if(e.CommandArgument == "Prev"){//上条
			if(Convert.ToInt32(CurrentRecord.Text) > 1){
				CurrentRecord.Text=(Convert.ToInt32(CurrentRecord.Text) - 1).ToString();
				NowRecord = Convert.ToInt32(CurrentRecord.Text)-1;
			}
			else{
				NowRecord = 1;
			}
		}
		ShowRecord();
	}
	void GotoRecord(Object sender,EventArgs e){
		try{
			int k = Convert.ToInt32(ToRecord.Text);//取得输入之数字
			if(k>Convert.ToInt32(AllRecords.Text)){//若输入大于总条数
				k = Convert.ToInt32(AllRecords.Text);
			}
			if(k<0){//若输入小于0
				k = 1;
			}
			NowRecord=k-1;
			ToRecord.Text=k.ToString();
			CurrentRecord.Text=k.ToString();
			ShowRecord();
		}
		catch{
			NowRecord=0;
			ToRecord.Text="1";
			CurrentRecord.Text="1";
			ShowRecord();
		}
	}
	void Orders_Edit(object sender,DataListCommandEventArgs e){
		Orders.EditItemIndex = e.Item.ItemIndex;
		bindNew(false);
		TextBox f=(TextBox)Orders.Items[0].FindControl("OrderID");
		f.Visible=false;
		DataSet DS = (DataSet)Session["NowDO"];
		DataRow NRow = DS.Tables["OrdersN"].Rows[0];
		string h=NRow["CusID"].ToString();
		Control k=(Control)Orders;
		AddOrder.Visible=false;
		Status.Text="编辑记录";
		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=NRow["CusID"].ToString();
		x.SessionName="NowSTKSQL";
		x.CompareField="CusID";
		x.Show();//显示Drop.ascx
	}
	void Orders_Update(object sender,DataListCommandEventArgs e){
		DataSet DS = (DataSet)Session["NowDO"];
		string w="";
		TextBox BeforeTaxPrice = (TextBox)e.Item.FindControl("BeforeTaxPrice");
		TextBox TaxPrice = (TextBox)e.Item.FindControl("TaxPrice");
		TextBox TotalPrice = (TextBox)e.Item.FindControl("TotalPrice");
		Double k=Convert.ToDouble(TaxPrice.Text) + Convert.ToDouble(BeforeTaxPrice.Text);//取得含税价
		ASP.Drop_ascx x;
		x=(ASP.Drop_ascx)Orders.Items[Orders.EditItemIndex].FindControl("CusID");
		ASP.Cal_ascx a;
		a=(ASP.Cal_ascx)Orders.Items[Orders.EditItemIndex].FindControl("OrderDate");
		int y=0;
		if(TaxPrice.Text!="0"){//若不等于0则y等于1
			y=1;//y是含税否
		}
		if(Status.Text=="编辑记录"){
			Label NowID = (Label)e.Item.FindControl("OrderIDV");
			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{
			TextBox Nowid = (TextBox)e.Item.FindControl("OrderID");
			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 + "')";
		}
		try{
			SqlCommand UpOrder=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(Exception p){
			errMsg.Text="以" + w + "添加失败" + p.ToString();
		}
	}
	void Orders_Cancel(object sender,DataListCommandEventArgs e){
		if(Status.Text=="添加记录"){
			Status.Text="查看记录";
			int x=Convert.ToInt32(AllRecords.Text);
			NowRecord=x-2;
			AllRecords.Text=(x-1).ToString();
			CurrentRecord.Text=(x-1).ToString();
			ShowRecord();
		}
		Orders.EditItemIndex = -1;
		AddOrder.Visible=true;
		bindNew(false);
	}
	void AddOrder_Click(Object sender,EventArgs e){
		NowRecord=-1;
		ShowRecord();
		Status.Text="添加记录";
		Label f=(Label)Orders.Items[0].FindControl("OrderIDV");
		f.Visible=false;//隐藏
		int x=Convert.ToInt32(AllRecords.Text)+1;
		AllRecords.Text=x.ToString();//总条数加1
		CurrentRecord.Text=x.ToString();
		AddOrder.Visible=false;
	}
	Double Teststring(string h){
		if(h==""){
			return 0;
		}
		else{
			return Convert.ToDouble(h);
		}
	}
	</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/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: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>