<%@ Register TagPrefix="Title" TagName="TitleShow" Src="/aspnetdata/Title.ascx" %>
<%@Page Debug=True responseEncoding="utf-8"%>
<%@Import Namespace="System.Threading"%>
<%@Import Namespace="System.Globalization"%>
<%@ 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 * from Product order by ProductID";
	int NowRecord;//目前条数
	void Page_load(object sender,EventArgs e){
		Source = (SqlConnection)Session["NowSTKSQL"];
		if(IsPostBack==false){
			Hashtable HT = new Hashtable();
			HT.Add("繁体中文", "zh-TW");
			HT.Add("简体中文", "zh-CN");
			HT.Add("英文-美国", "en-US");
			AllLang.DataSource = HT;
			AllLang.DataValueField="Value";
			AllLang.DataTextField="Key";
			AllLang.DataBind();
			Save.Visible=false;//隐藏保存按钮
			ErrorID.Visible=false;//隐藏产品编号错误的Label
			UndoR.Visible=false;//隐藏取消按钮
			CurrentRecord.Text="1";//目前条数为1
			NowRecord = 0; //取得第一条记录
			EditRecord.Visible=false;//隐藏编辑的Panel
			bindNew();
statusHidden.Text="0";
Page.DataBind();
			}
	}
	void bindNew(){
			DataSet DS = new DataSet();
			SqlDataAdapter NComm = new SqlDataAdapter(d, Source);
			NComm.Fill(DS, "Product");
			AllBook.DataSource=DS.Tables["Product"].DefaultView;
			AllBook.DataTextField="ProductName";
			AllBook.DataValueField="ProductID";
			AllBook.DataBind();//结合至供应商的DropDownlist
			Session["NowDSPo"]=DS;//声明为Session
			ChangeLang();
			Status.Text=GetText("View");//"查看记录";
			AllRecords.Text=DS.Tables["Product"].Rows.Count.ToString();
			ShowRecord();
	}
	void ShowRecord(){
		DataSet NowDS = (DataSet)Session["NowDSPo"];
		if(NowRecord >= 0){ //非添加时
			DataRow NowRow = NowDS.Tables["Product"].Rows[NowRecord];
			ProductIDV.Text=NowRow["ProductID"].ToString();
			ProductID.Text=NowRow["ProductID"].ToString();
			Pages.Text=NowRow["Pages"].ToString();
			PagesV.Text=NowRow["Pages"].ToString();
			ProductName.Text=NowRow["ProductName"].ToString();
			ProductNameV.Text=NowRow["ProductName"].ToString();
			PriceCus.Text=NowRow["PriceCus"].ToString();
			PriceCusV.Text=NowRow["PriceCus"].ToString();
			delH.NavigateUrl="javascript:doconfirm('确定删除" + ProductName.Text + "?','DelR','')";
			if(NowRow.IsNull("Photo")==false){//检查有无图形文件名
				PhotoFile.Text=NowRow["Photo"].ToString();
				ShowPhoto.HRef="javascript:Show('" + NowRow["Photo"].ToString() + "')";
			}
			else{
				PhotoFile.Text="";
				ShowPhoto.HRef="";
			}
			StockSafe.Text=NowRow["StockSafe"].ToString();
			StockSafeV.Text=NowRow["StockSafe"].ToString();
			StockCurrent.Text=NowRow["StockCurrent"].ToString();
			StockCurrentV.Text=NowRow["StockCurrent"].ToString();
			PhotoV.ImageUrl=NowRow["Photo"].ToString();
			Photo.ImageUrl=NowRow["Photo"].ToString();
		}
		else{
			ProductName.Text="";
			Pages.Text="";
			PriceCus.Text="";
			StockSafe.Text="";
			StockCurrent.Text="";
			PhotoFile.Text="";
			Photo.ImageUrl="";
		}
	}
	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 Record(Object sender,CommandEventArgs e){
		if(e.CommandArgument == "ToEdit"){
			View.Visible=false;//隐藏显示的Panel
			EditRecord.Visible=true;//显示编辑的Panel
			Save.Visible=true;//显示保存按钮
			UndoR.Visible=true;//显示取消按钮
			EditOrView.Visible=false;//隐藏编辑按钮
			AddNewR.Visible=false;//隐藏添加按钮
			delH.Visible=false;//隐藏删除按钮
			ChangeLang();
			Status.Text=GetText("Edit");//"编辑记录";
		}
		else if(e.CommandArgument == "UndoR"){
			if(statusHidden.Text=="-1"){//若为添加状态,回至第一条
				NowRecord=0;
				CurrentRecord.Text="1";
				AllRecords.Text = (Convert.ToInt32(AllRecords.Text) - 1).ToString();
			}
			else{//回至目前条数
				NowRecord = Convert.ToInt32(CurrentRecord.Text)-1;
			}
			View.Visible=true;
			EditRecord.Visible=false;
			Save.Visible=false;
			UndoR.Visible=false;
			EditOrView.Visible=true;
			AddNewR.Visible=true;
			delH.Visible=true;
			ChangeLang();
			statusHidden.Text="0";
			Status.Text=GetText("View");//"查看记录";
			ShowRecord();
		}
		else if(e.CommandArgument == "AddNewR"){
			CurrentRecord.Text=(Convert.ToInt32(AllRecords.Text) + 1).ToString();
			AllRecords.Text = (Convert.ToInt32(AllRecords.Text) + 1).ToString();
			EditRecord.Visible=true;//显示编辑的Panel
			View.Visible=false;//隐藏查看的Panel
			NowRecord = -1;//设为-1，表示将执行添加
			Save.Visible=true;//显示保存按钮
			delH.Visible=false;//隐藏删除按钮
			UndoR.Visible=true;//显示取消按钮
			EditOrView.Visible=false;//隐藏编辑按钮
			ProductID.Visible=false;
			ProductIDT.Visible=true;
			ProductIDT.Text="";
			statusHidden.Text="-1";
			ProductName.Text="";
			PriceCus.Text="0";
			Pages.Text="0";
			StockSafe.Text="0";
			StockCurrent.Text="0";
			ChangeLang();
			Status.Text=GetText("New");//显示添加
			Photo.ImageUrl="";
			PhotoFile.Text="";
			AddNewR.Visible=false;//隐藏添加按钮
		}
		else if(e.CommandArgument == "SaveR"){
			string h;
			DataSet NowDS = (DataSet)Session["NowDSPo"];
			if(statusHidden.Text=="-1"){
				if(ProductIDT.Text==""){
					ErrorID.Text="未输入编号，无法保存";
					ErrorID.Visible=true;
					return;
				}
				else{
					h="Insert Into Product (ProductID,ProductName,Pages,PriceCus,StockSafe,StockCurrent,Photo) Values (";
					h+="'" + ProductIDT.Text + "','" + ProductName.Text + "'," + Pages.Text + "," + PriceCus.Text + "," + StockSafe.Text + "," + StockCurrent.Text + ",'" + PhotoFile.Text + "')";
				}
			}
			else{
				h="Update Product Set ProductName = '" + ProductName.Text + "',Pages =" + Pages.Text + ",StockSafe =" + StockSafe.Text + ",StockCurrent =" + StockCurrent.Text + ",PriceCus =" + PriceCus.Text + ",Photo ='" + PhotoFile.Text + "'";
				h+=" where ProductID ='" + ProductID.Text + "'";
			}
			try{
				SqlCommand UpBook=new SqlCommand(h,Source);
				UpBook.Connection.Open();
				UpBook.ExecuteNonQuery();//执行保存
				UpBook.Connection.Close();
				ChangeLang();
				Status.Text=GetText("View");
  				View.Visible=true;
				EditRecord.Visible=false;
				Save.Visible=false;
				EditOrView.Visible=true;
				statusHidden.Text="0";
				AddNewR.Visible=true;
				delH.Visible=true;
				UndoR.Visible=false;
				bindNew();
				NowRecord = Convert.ToInt32(CurrentRecord.Text)-1;
				ShowRecord();
				getAllBookSource();
			}
			catch{
				ChangeLang();
				errMsg.Text=GetText("ErrSave") + h;
			}
		}
		else if(e.CommandArgument == "DelR"){
			string h="delete from Product where ProductID = '" + ProductID.Text + "'";
			SqlCommand UpBook=new SqlCommand(h,Source);
			UpBook.Connection.Open();
			UpBook.ExecuteNonQuery();//执行保存
			UpBook.Connection.Close();
			NowRecord=Convert.ToInt32(CurrentRecord.Text)-1;
			if(NowRecord>0){
				NowRecord-=1;
			}
			CurrentRecord.Text=(Convert.ToInt32(AllRecords.Text) - 1).ToString();
			AllRecords.Text = (Convert.ToInt32(AllRecords.Text) - 1).ToString();
			ShowRecord();
		}
	}
	void ChangeBook(Object sender,EventArgs e){
		NowRecord=AllBook.SelectedIndex;
		ShowRecord();
	}
	void ExecUpload(Object sender,EventArgs e){
		try{
		if(PhotoF.PostedFile!=null){
			int a = PhotoF.PostedFile.FileName.IndexOf(":");//取得位置
			string x=PhotoF.PostedFile.FileName.Remove(0,a+2);//截字符串
			string y=Request.PhysicalPath.Remove(Request.PhysicalPath.Length-9,9);
			PhotoF.PostedFile.SaveAs(y + x);
			PhotoFile.Text=x;
			Photo.ImageUrl=x;
		}
		}catch
		{}
	}
	void ClearPho(Object sender,EventArgs e){
		PhotoFile.Text="";
		Photo.ImageUrl="";
	}
	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 getAllBookSource(){
		DataSet DS = new DataSet();
		SqlDataAdapter NComm = new SqlDataAdapter(d, Source);
		NComm.Fill(DS, "Product");
		AllBook.DataSource=DS.Tables["Product"].DefaultView;
		AllBook.DataTextField="ProductName";
		AllBook.DataValueField="ProductID";
		AllBook.DataBind();//结合至供应商的DropDownlist
	}
	string GetText(string GID){
		string NowCul=Thread.CurrentThread.CurrentCulture.Name;//目前语言
		string g="Select DisplayText from AllCultureDisplay where Culture ='" + NowCul + "' and InterfaceID ='" + GID + "'";
		DataSet DS = new DataSet();
		SqlDataAdapter NComm = new SqlDataAdapter(g, Source);
		NComm.Fill(DS, GID);
		return DS.Tables[GID].Rows[0][0].ToString();
	}
	void OK_Click(object sender,EventArgs e){
		ChangeLang();
		Page.DataBind();
		string NowCul=Thread.CurrentThread.CurrentCulture.Name;
		string g="Select * from AllCultureDisplay";//更新目前状态文字
		DataSet DS = new DataSet();
		SqlDataAdapter NComm = new SqlDataAdapter(g, Source);
		NComm.Fill(DS, "test");
		DS.Tables["test"].DefaultView.RowFilter="DisplayText ='" + Status.Text + "'";
		string v=DS.Tables["test"].DefaultView[0]["InterfaceID"].ToString();
		DS.Tables["test"].DefaultView.RowFilter="InterfaceID ='" + v + "' and Culture ='" + NowCul + "'";
		Status.Text=DS.Tables["test"].DefaultView[0]["DisplayText"].ToString();
	}
	void ChangeLang(){
		CultureInfo cult;//声明新语言
		cult = new CultureInfo(AllLang.SelectedItem.Value);
		Thread.CurrentThread.CurrentCulture = cult;//更改语言
	}
	</script>
<script language="javascript">
<!--
function doconfirm(ask,arg1,arg2) {
	if(confirm(ask)) __doPostBack(arg1,arg2);
}
function Show(FName) {
window.open(FName,null,"height=350,width=280,ststus=no,location=no");
}
// -->
</script>
<link rel=stylesheet type="text/css" href="Style.css">
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form enctype="multipart/form-data" runat="server">
<asp:label id="statusHidden" visible="false" runat="server" />
<asp:Label id="SelectLang" Runat="server" Text='<%# GetText("SelectLang") %>'/><asp:dropdownlist id="AllLang" runat="server" />
<asp:Button Text="OK" runat="server" onclick="OK_Click" /><br>
<asp:Label id="SelectBook" Runat="server" Text='<%# GetText("SelectBook") %>' /><asp:dropdownlist id="AllBook" autopostback="true" OnSelectedIndexChanged="ChangeBook" runat="server" /><br>
<asp:LinkButton ID="First" Tooltip="First" OnCommand="ChangeRecord" Runat="server" Text='<%# GetText("First") %>' CommandArgument="First" cssclass="Tab_08" />
<asp:LinkButton ID="Prev" Tooltip="Prev" OnCommand="ChangeRecord" Runat="server" Text='<%# GetText("Prev") %>' CommandArgument="Prev"  cssclass="Tab_08"/>
<asp:LinkButton ID="Next" Tooltip="Next" OnCommand="ChangeRecord" Runat="server" Text='<%# GetText("Next") %>' CommandArgument="Next"  cssclass="Tab_08"/>
<asp:LinkButton ID="Last" Tooltip="Last" OnCommand="ChangeRecord" Runat="server" Text='<%# GetText("Last") %>' CommandArgument="Last"  cssclass="Tab_08"/>
<asp:Label id="GotoRe" Text='<%# GetText("GotoRe") %>' Runat="server" /><asp:TextBox Columns="3" AutoPostBack="true" ID="ToRecord" OnTextChanged="GotoRecord" Runat="server" /><asp:Label id="Rec" Text='<%# GetText("Rec") %>' Runat="server" />
<asp:LinkButton ID="EditOrView" Tooltip="EditOrView" OnCommand="Record" Runat="server" Text='<%# GetText("EditOrView") %>' CommandArgument="ToEdit" cssclass="Tab_08" />
<asp:LinkButton ID="UndoR" Tooltip="UndoR" OnCommand="Record" Runat="server" Text='<%# GetText("UndoR") %>' CommandArgument="UndoR" cssclass="Tab_08" />
<asp:LinkButton ID="Save" Tooltip="Save" OnCommand="Record" Runat="server" Text='<%# GetText("Save") %>' CommandArgument="SaveR" cssclass="Tab_08" />
<asp:LinkButton ID="AddNewR" Tooltip="AddNewR" OnCommand="Record" Runat="server" Text='<%# GetText("AddNewR") %>' CommandArgument="AddNewR" cssclass="Tab_08" />
<asp:LinkButton ID="DelR" Tooltip="DelR" Visible="False" OnCommand="Record" Runat="server" Text='<%# GetText("DelR") %>' CommandArgument="DelR" cssclass="Tab_08" />
<asp:Hyperlink id="delH" Text='<%# GetText("DelR") %>' runat="server" cssclass="Tab_08"/>
<p><asp:Label id="NoR" Runat="server" Text='<%# GetText("NoR") %>' /><asp:Label ID="CurrentRecord" Tooltip="CurrentRecord" Runat="server" /><asp:Label id="Rec1" Text='<%# GetText("Rec") %>' Runat="server" /><asp:Label id="RecT" Text='<%# GetText("RecT") %>' Runat="server" /><asp:Label ID="AllRecords" Tooltip="AllRecords" Runat="server" /><asp:Label id="Rec2" Text='<%# GetText("Rec") %>' Runat="server" />
<font color="#FF0000"><b><asp:Label id="NowStatus" Runat="server" Text='<%# GetText("NowStatus") %>' /><asp:label id="Status" Tooltip="status" runat="server" /></b></font>
<font color="#FF0000"><b><asp:label id="errMsg" runat="server" /></b></font><br>
<hr>
<asp:Panel ID="View" width="550" BorderWidth="2" BorderColor="Black" BackColor="lightyellow" Runat="server">
<table cellspacing="1" cellpadding="2" ><tr><td class="Tab_IP8_3">
<p><asp:Label id="BookID" Runat="server" Text='<%# GetText("BookID") %>'/><asp:Label ID="ProductIDV" Tooltip="ProductIDV" Runat="server" cssclass="Tab_07" width="180"/>
<p><asp:Label id="BookName" Runat="server" Text='<%# GetText("BookName") %>'/><asp:Label ID="ProductNameV" Tooltip="ProductNameV" Runat="server" cssclass="Tab_07" width="180"/>
<p><asp:Label id="TotalPages" Runat="server" Text='<%# GetText("TotalPages") %>'/><asp:Label ID="PagesV" Tooltip="UnitV" Runat="server" cssclass="Tab_07" width="180"/>
<p><asp:Label id="UPrice" Runat="server" Text='<%# GetText("UPrice") %>'/><asp:Label ID="PriceCusV" Tooltip="PriceInV" Runat="server" cssclass="Tab_07" width="180"/>
<p><asp:Label id="Stock" Runat="server" Text='<%# GetText("Stock") %>'/><asp:Label ID="StockSafeV" Tooltip="StockSafeV" Runat="server" cssclass="Tab_07" width="180"/>
<p><asp:Label id="StockC" Runat="server" Text='<%# GetText("StockC") %>'/><asp:Label ID="StockCurrentV" Tooltip="StockCurrentV" Runat="server" cssclass="Tab_07" width="180"/>
</td>
<td valign="top" class="Tab_IP8_3"><p><asp:Label id="Cover" Runat="server" Text='<%# GetText("Cover") %>'/><br><a id="ShowPhoto" runat="server"><asp:Image ID="PhotoV" width="120" height="170" Tooltip="PhotoV" Runat="server" /></a></td></tr></table>
</asp:Panel>
<asp:Panel ID="EditRecord" width="550" BorderWidth="2" BorderColor="Black" BackColor="lightyellow" Runat="server" >
<table cellspacing="1" cellpadding="2" ><tr><td class="Tab_IP8_3">
<p><asp:Label id="BookID1" Runat="server" Text='<%# GetText("BookID") %>'/><asp:Label width="150"　ID="ProductID" Tooltip="ProductID" Runat="server" /><asp:textbox ID="ProductIDT" Columns="8" Tooltip="ProductIDT" Visible="False" Runat="server" /><font color="#FF0000"><b><asp:Label ID="ErrorID" Tooltip="ErrorID" Runat="server" /></b></font>
<p><asp:Label id="BookName1" Runat="server" Text='<%# GetText("BookName") %>'/><asp:TextBox columns="16" width="150" ID="ProductName" Tooltip="ProductName" Runat="server" />
<p><asp:Label id="TotalPages1" Runat="server" Text='<%# GetText("TotalPages") %>'/><asp:TextBox ID="Pages" Runat="server" width="150"/>
<p><asp:Label id="UPrice1" Runat="server" Text='<%# GetText("UPrice") %>'/><asp:TextBox ID="PriceCus" Tooltip="PriceCus" Runat="server" width="150"/>
<p><asp:Label id="Stock1" Runat="server" Text='<%# GetText("Stock") %>'/><asp:TextBox ID="StockSafe" Tooltip="StockSafe" Runat="server" width="150"/>
<p><asp:Label id="StockC1" Runat="server" Text='<%# GetText("StockC") %>'/><asp:TextBox ID="StockCurrent" Tooltip="StockCurrent Runat="server" width="150"/>
</td>
<td valign="top" width="50%" class="Tab_IP8_3">
<p><asp:Label id="Cover1" Runat="server" Text='<%# GetText("Cover") %>'/><input id="PhotoF" Tooltip="Photo" accept="image/*" type="file" runat="server" size="20"><font color="#FF0000"><b><asp:Label ID="ErrorFile" Tooltip="ErrorFile" Runat="server" /></b></font>
<input type=button id="Button1" value="Upload" OnServerClick="ExecUpload" runat="server">
<input type=button id="Button2" value="Clear" OnServerClick="ClearPho" runat="server">
<br><asp:Label id="PhotoF1" Runat="server" Text='<%# GetText("PhotoF") %>'/><asp:Label ID="PhotoFile" Runat="server" /><br>
<asp:Image ID="Photo" Tooltip="Photo" Runat="server" />
</td></tr></table>
</asp:Panel>
</form>