<%@ 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="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){
			Save.Visible=false;//隐藏保存按钮
			ErrorID.Visible=false;//隐藏产品编号错误的Label
			UndoR.Visible=false;//隐藏取消按钮
			CurrentRecord.Text="1";//目前条数为1
			NowRecord = 0; //取得第一条记录
			EditRecord.Visible=false;//隐藏编辑之Panel
			bindNew();
			}
	}
	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
			Status.Text="查看记录";
			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;//隐藏删除按钮
			Status.Text="编辑记录";
		}
		else if(e.CommandArgument == "UndoR"){
			if(Status.Text=="添加记录"){//若为添加状态,回至第一条
				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;
			Status.Text="查看记录";
			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="";
			ProductName.Text="";
			PriceCus.Text="0";
			Pages.Text="0";
			StockSafe.Text="0";
			StockCurrent.Text="0";
			Status.Text="添加记录";
			Photo.ImageUrl="";
			PhotoFile.Text="";
			AddNewR.Visible=false;//隐藏添加按钮
		}
		else if(e.CommandArgument == "SaveR"){
			string h;
			DataSet NowDS = (DataSet)Session["NowDSPo"];
			if(Status.Text=="添加记录"){
				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();
				Status.Text="查看记录";
  				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{
					errMsg.Text="--发生错误，无法保存--"+ 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
	}
	</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>