<%@ Register TagPrefix="Title" TagName="TitleShow" Src="/aspnetdata/Title.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 * 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
			Save.Visible=false '隐藏保存按钮
			ErrorID.Visible=false '隐藏产品编号错误的Label
			UndoR.Visible=false '隐藏取消按钮
			CurrentRecord.Text="1" '目前条数为1
			NowRecord = 0  '取得第一条记录
			EditRecord.Visible=false '隐藏编辑的Panel
			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, "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
			Status.Text="查看记录"
			AllRecords.Text=DS.Tables("Product").Rows.Count.ToString()
			ShowRecord()
		end if
	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 '隐藏删除按钮
			Status.Text="编辑记录"
		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
			Status.Text="查看记录"
			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"
			Status.Text="添加记录"
			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()
				Status.Text="查看记录"
  				View.Visible=true
				EditRecord.Visible=false
				Save.Visible=false
				EditOrView.Visible=true
				AddNewR.Visible=true
				delH.Visible=true
				UndoR.Visible=false
				bindNew(true)
				NowRecord = Convert.ToInt32(CurrentRecord.Text)-1
				ShowRecord()
				getAllBookSource()
			catch
					errMsg.Text="--发生错误，无法保存--"
			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)
		dim k as integer = Convert.ToInt32(ToRecord.Text)
		if k>Convert.ToInt32(AllRecords.Text) then
			k = Convert.ToInt32(AllRecords.Text)
		end if
		NowRecord=k-1
		ToRecord.Text=k.ToString()
		CurrentRecord.Text=k.ToString()
		ShowRecord()
	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
	</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:dropdownlist id="AllBook" autopostback="true" OnSelectedIndexChanged="ChangeBook" runat="server" /><br>
<asp:LinkButton ID="First" Tooltip="First" OnCommand="ChangeRecord" Runat="server" Text="首条" CommandArgument="First" cssclass="Tab_08" />
<asp:LinkButton ID="Prev" Tooltip="Prev" OnCommand="ChangeRecord" Runat="server" Text="上条" CommandArgument="Prev"  cssclass="Tab_08"/>
<asp:LinkButton ID="Next" Tooltip="Next" OnCommand="ChangeRecord" Runat="server" Text="下条" CommandArgument="Next"  cssclass="Tab_08"/>
<asp:LinkButton 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" />条
<asp:LinkButton ID="EditOrView" Tooltip="EditOrView" OnCommand="Record" Runat="server" Text="编辑" CommandArgument="ToEdit" cssclass="Tab_08" />
<asp:LinkButton ID="UndoR" Tooltip="UndoR" OnCommand="Record" Runat="server" Text="取消" CommandArgument="UndoR" cssclass="Tab_08" />
<asp:LinkButton ID="Save" Tooltip="Save" OnCommand="Record" Runat="server" Text="保存" CommandArgument="SaveR" cssclass="Tab_08" />
<asp:LinkButton ID="AddNewR" Tooltip="AddNewR" OnCommand="Record" Runat="server" Text="添加" CommandArgument="AddNewR" cssclass="Tab_08" />
<asp:LinkButton ID="DelR" Tooltip="DelR" Visible="False" OnCommand="Record" Runat="server" Text="删除" CommandArgument="DelR" cssclass="Tab_08" />
<asp:Hyperlink id="delH" Text="删除" runat="server" cssclass="Tab_08"/>
<p>第<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>
<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="ProductIDV" Tooltip="ProductIDV" Runat="server" cssclass="Tab_07" width="180"/>
<p>书籍名称：<asp:Label ID="ProductNameV" Tooltip="ProductNameV" Runat="server" cssclass="Tab_07" width="180"/>
<p>页　　数：<asp:Label ID="PagesV" Tooltip="UnitV" Runat="server" cssclass="Tab_07" width="180"/>
<p>建议售价：<asp:Label ID="PriceCusV" Tooltip="PriceInV" Runat="server" cssclass="Tab_07" width="180"/>
<p>安全库存：<asp:Label ID="StockSafeV" Tooltip="StockSafeV" Runat="server" cssclass="Tab_07" width="180"/>
<p>目前库存：<asp:Label ID="StockCurrentV" Tooltip="StockCurrentV" Runat="server" cssclass="Tab_07" width="180"/>
</td>
<td valign="top" class="Tab_IP8_3"><p>封面：<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 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:TextBox columns="16" width="150" ID="ProductName" Tooltip="ProductName" Runat="server" />
<p>页　　数：<asp:TextBox ID="Pages" Runat="server" width="150"/>
<p>建议售价：<asp:TextBox ID="PriceCus" Tooltip="PriceCus" Runat="server" width="150"/>
<p>安全库存：<asp:TextBox ID="StockSafe" Tooltip="StockSafe" Runat="server" width="150"/>
<p>目前库存：<asp:TextBox ID="StockCurrent" Tooltip="StockCurrent Runat="server" width="150"/>
</td>
<td valign="top" width="50%" class="Tab_IP8_3">
<p>封面：<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="执行上传" OnServerClick="ExecUpload" runat="server">
<input type=button id="Button2" value="清除照片" OnServerClick="ClearPho" runat="server">
<br>图形档名称：<asp:Label ID="PhotoFile" Runat="server" /><br>
<asp:Image ID="Photo" Tooltip="Photo" Runat="server" />
</td></tr></table>
</asp:Panel>
</form>