<%@ 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="vb" runat="server">
	Dim Source as SqlConnection
	dim d as string ="SELECT * from Product order by ProductID"
	DIm NowRecord as Integer
	sub Page_load(sender as object,e as EventArgs)
		Source = Ctype(Session("NowSTKSQL"),SqlConnection)
		if IsPostBack=false then
			Dim HT as Hashtable = 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()
		end if
	end sub
	sub bindNew()
		DIm DS as DataSet = new DataSet()
		Dim NComm as SqlDataAdapter = new SqlDataAdapter(d, Source)
		NComm.Fill(DS, "Product")
		AllBook.DataSource=DS.Tables("Product").DefaultView
		AllBook.DataTextField="ProductName"
		AllBook.DataValueField="ProductID"
		AllBook.DataBind()'结合至供应商的DropDownlist
		Dim PmyKey(1) as DataColumn
		PmyKey(0) = DS.Tables("Product").Columns("ProductID")
		DS.Tables("Product").PrimaryKey = PmyKey
		Session("NowDSPo")=DS'声明为Session
		ChangeLang()
		Status.Text=GetText("View")'查看记录"
		AllRecords.Text=DS.Tables("Product").Rows.Count.ToString()
		ShowRecord()
	end sub
	sub ShowRecord()
		Dim NowDS as DataSet = Ctype(Session("NowDSPo"),DataSet)
		if NowRecord = -1 then '添加记录时
			ProductName.Text=""
			Pages.Text=""
			PriceCus.Text=""
			StockSafe.Text=""
			StockCurrent.Text=""
			PhotoFile.Text=""
			Photo.ImageUrl=""
		else
			Dim NowRow as DataRow = 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 then
				PhotoFile.Text=NowRow("Photo").ToString()
				ShowPhoto.HRef="javascript:Show('" + NowRow("Photo").ToString() + "')"
			else
				PhotoFile.Text=""
				ShowPhoto.HRef=""
			end if
			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()
		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
			ShowRecord()
		elseif e.CommandArgument = "First" then '首条
			CurrentRecord.Text="1"
			NowRecord = 0
			ShowRecord()
		elseif e.CommandArgument = "Last" then '末条
			CurrentRecord.Text=(Convert.ToInt32(AllRecords.Text)).ToString()
			NowRecord = Convert.ToInt32(AllRecords.Text) - 1
			ShowRecord()
		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
			ShowRecord()
		end if
	end sub
	sub Record(sender as Object,e as CommandEventArgs)
		if e.CommandArgument = "ToEdit" then
			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")'编辑记录
		elseif e.CommandArgument = "UndoR" then
			if Status.Text="添加记录" then
				NowRecord=0
				CurrentRecord.Text="1"
				AllRecords.Text = (Convert.ToInt32(AllRecords.Text) - 1).ToString()
			else
				NowRecord = Convert.ToInt32(CurrentRecord.Text)-1
			end if
			View.Visible=true
			EditRecord.Visible=false
			Save.Visible=false
			UndoR.Visible=false
			EditOrView.Visible=true
			AddNewR.Visible=true
			delH.Visible=true
			statusHidden.Text="0"
			Status.Text=GetText("View")'查看记录
			ShowRecord()
		elseif e.CommandArgument = "AddNewR" then
			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=""
			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'隐藏添加按钮
		elseif e.CommandArgument = "SaveR" then
			dim h as string=""
			Dim NowDS as DataSet = Ctype(Session("NowDSPo"),DataSet)
			if Status.Text="添加记录" then
				if ProductIDT.Text="" then
					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 & "')"
				end if
			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 & "'"
			end if
			try
				Dim UpBook as SqlCommand = 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
				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
			end try
		elseif e.CommandArgument = "DelR" then
			dim h as string="delete from Product where ProductID = '" & ProductID.Text & "'"
			Dim UpBook as SqlCommand=new SqlCommand(h,Source)
			UpBook.Connection.Open()
			UpBook.ExecuteNonQuery()'执行保存
			UpBook.Connection.Close()
			NowRecord=Convert.ToInt32(CurrentRecord.Text)-1
			if NowRecord>0 then
				NowRecord-=1
			end if
			CurrentRecord.Text=(Convert.ToInt32(AllRecords.Text) - 1).ToString()
			AllRecords.Text = (Convert.ToInt32(AllRecords.Text) - 1).ToString()
			ShowRecord()
		end if
	end sub
	sub ChangeBook(sender as Object,e as EventArgs)
		NowRecord=AllBook.SelectedIndex
		ShowRecord()
	end sub
	sub ExecUpload(sender as Object,e as EventArgs)
		try
		if Not IsNothing(PhotoF.PostedFile) then
			dim a as integer = PhotoF.PostedFile.FileName.IndexOf(":")'取得位置
			dim x as string = PhotoF.PostedFile.FileName.Remove(0,a+2)'截字符串
			dim y as string = Request.PhysicalPath.Remove(Request.PhysicalPath.Length-9,9)
			PhotoF.PostedFile.SaveAs(y + x)
			PhotoFile.Text=x
			Photo.ImageUrl=x
		end if
		catch
		end try
	end sub
	sub ClearPho(sender as Object,e as EventArgs )
		PhotoFile.Text=""
		Photo.ImageUrl=""
	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 getAllBookSource()
		dim DS as DataSet = new DataSet()
		Dim NComm as SqlDataAdapter = new SqlDataAdapter(d, Source)
		NComm.Fill(DS, "Product")
		AllBook.DataSource=DS.Tables("Product").DefaultView
		AllBook.DataTextField="ProductName"
		AllBook.DataValueField="ProductID"
		AllBook.DataBind()'结合至供应商的DropDownlist
	end sub
	Function GetText(GID as string) as string
		Dim NowCul as string =Thread.CurrentThread.CurrentCulture.Name'目前语言
		dim g as string ="Select DisplayText from AllCultureDisplay where Culture ='" & NowCul & "' and InterfaceID ='" & GID & "'"
		Dim DS as DataSet = new DataSet()
		Dim NComm as SqlDataAdapter = new SqlDataAdapter(g, Source)
		NComm.Fill(DS, GID)
		return DS.Tables(GID).Rows(0)(0).ToString()
	end function
	sub OK_Click(sender as object,e as EventArgs)
		ChangeLang()
		Page.DataBind()
		dim NowCul as string =Thread.CurrentThread.CurrentCulture.Name
		dim g as string ="Select * from AllCultureDisplay"'更新目前状态文字
		Dim DS as DataSet = new DataSet()
		Dim NComm as SqlDataAdapter = new SqlDataAdapter(g, Source)
		NComm.Fill(DS, "test")
		DS.Tables("test").DefaultView.RowFilter="DisplayText ='" & Status.Text & "'"
		dim v as string =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()
	end sub
	sub ChangeLang()
		Dim cult as CultureInfo'声明新语言
		cult = new CultureInfo(AllLang.SelectedItem.Value)
		Thread.CurrentThread.CurrentCulture = cult'更改语言
	end sub
	</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>