<%@ 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";
	void Page_load(object sender,EventArgs e){
		Source = (SqlConnection)Session["NowSTKSQL"];
		if(IsPostBack==false){
			bindNew(true);
			Panel1.Visible=false;
			}
	}
	void bindNew(Boolean tag){//结合至DataGrid
		if(tag==true){
			DataSet DS = new DataSet();
			SqlDataAdapter NComm = new SqlDataAdapter(d, Source);
			NComm.Fill(DS, "Product");
			DataColumn[] Pmykey = new DataColumn[1]; //声明新主键数组
			Pmykey[0] = DS.Tables["Product"].Columns["ProductID"];
			DS.Tables["Product"].PrimaryKey = Pmykey; //加入主键
			Session["NowDSE"]=DS;//声明为Session
			Session["NowComm"]=NComm;
			AllProduct.DataSource=DS.Tables["Product"].DefaultView;
			AllProduct.DataBind();
		}
		else{
			DataSet DS=(DataSet)Session["NowDSE"];
			AllProduct.DataSource=DS.Tables["Product"].DefaultView;
			AllProduct.DataBind();
		}
	}
	void AllProduct_Edit(object sender,DataListCommandEventArgs e){
		AllProduct.SelectedIndex = e.Item.ItemIndex; //设定选取记录
		Panel1.Visible=true; //打开Panel
		DataSet DS = (DataSet)Session["NowDSE"];
		Label x = (Label)e.Item.FindControl("PID");
		DataRow fRow = DS.Tables["Product"].Rows.Find(x.Text);
		ProductID.Text=fRow["ProductID"].ToString();
		ProductName.Text=fRow["ProductName"].ToString();
		Pages.Text=fRow["Pages"].ToString();
		PriceCus.Text=fRow["PriceCus"].ToString();
		StockCurrent.Text=fRow["StockCurrent"].ToString();
		Photo.ImageUrl=fRow["Photo"].ToString();
		Session["NowRow"]=fRow;
	}
	void CelClick(object sender,EventArgs e){
		AllProduct.SelectedIndex=-1;
		Panel1.Visible=false;
	}
	void OKClick(object sender,EventArgs e){
		DataSet DS = (DataSet)Session["NowDSE"];
		SqlDataAdapter NComm = (SqlDataAdapter)Session["NowComm"];
		DataRow ModiRow = (DataRow)Session["NowRow"];
		try{
			ModiRow["ProductName"]=ProductName.Text;
			ModiRow["PriceCus"]=PriceCus.Text;
			ModiRow["Pages"]=Pages.Text;
			ModiRow["StockCurrent"]=StockCurrent.Text;
			ModiRow["Photo"]=Photo.ImageUrl;
			SqlCommandBuilder custCB = new SqlCommandBuilder(NComm);
			NComm.Update(DS,"Product");//保存
			AllProduct.SelectedIndex = -1;
			bindNew(true);
			Panel1.Visible=false;
		}
		catch(Exception ex){
			errMsg.Text="发生错误" + ex.ToString();
		}
	}
	void ExecUpload(Object sender,EventArgs e){
		try{
		if(File1.PostedFile!=null){
			int a = File1.PostedFile.FileName.IndexOf(":");//取得位置
			string x=File1.PostedFile.FileName.Remove(0,a+2);//截字符串
			string y=Request.PhysicalPath.Remove(Request.PhysicalPath.Length-11,11);
			File1.PostedFile.SaveAs(y + x);
			Photo.ImageUrl=x;
		}
		}catch
		{}
	}
	void ClearPho(Object sender,EventArgs e){
		Photo.ImageUrl="";
	}
	</script>
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form enctype="multipart/form-data" runat="server">
<asp:label id="errMsg" runat="server" />
<table><tr><td>
<asp:DataList id="AllProduct" runat="server"
	BorderColor="black"	BorderWidth="1"
	GridLines="Both"	CellPadding="3"
	CellSpacing="0" 	Font-Name="Verdana"
	Font-Size="8pt" 	RepeatColumns="3"
	Width="500px"		HeaderStyle-BackColor="#C0FFFF"
	AlternatingItemStyle-BackColor="Gainsboro"
	RepeatDirection="Horizontal"
	SelectedItemStyle-BackColor="#FFFFC0"
	DataKeyField="ProductID"
	OnEditCommand="AllProduct_Edit"
            >
<ItemStyle Font-Size="X-Small" Width="150px"></ItemStyle>
	<HeaderTemplate>
                桂思强书籍列表
	</HeaderTemplate>
	<ItemTemplate>
		<asp:LinkButton id="button1" runat="server" Text="Edit" CommandName="edit" /><br>
		<asp:image width="80" Height="105" ImageURL='<%# DataBinder.Eval(Container.DataItem,"Photo") %>' id="img" Runat="server"/><br>		
		编号：<asp:label ID="PID" Text='<%# DataBinder.Eval(Container.DataItem,"ProductID")  %>' Runat="server" /><br>
		书名：<%# DataBinder.Eval(Container.DataItem,"ProductName")  %><br>
		目前库存：<%# DataBinder.Eval(Container.DataItem,"StockCurrent")  %><br>
	</ItemTemplate>
        </asp:DataList>
</td><td valign="top">
        <asp:Panel id="Panel1" runat="server"
            BackColor="#E0E0E0"
            Height="200px"
            Width="300px"
BorderWidth="2">
<br>　书籍编号：<asp:Label ID="ProductID" Runat="server" /><br>
　书籍名称：<asp:textbox ID="ProductName" Width="180" Runat="server" /><br>
　页数：<asp:textbox ID="Pages" Runat="server" Width="50"  /> 　
　建议售价：<asp:textbox ID="PriceCus" Runat="server" Width="50"/><br>
　目前库存：<asp:textbox Width="50" ID="StockCurrent" Runat="server" /><br>
　照片：<input id="File1" type=file runat="server" size="20">
<input type=button id="Button1" value="执行上传" OnServerClick="ExecUpload" runat="server">
<input type=button id="Button2" value="清除照片" OnServerClick="ClearPho" runat="server">
<br><asp:image id="Photo" Runat="server"/>
            <p><asp:Button ID="OK" text="保存" Onclick="OKClick" Runat="server" />　<asp:Button text="取消" Onclick="CelClick" ID="cel" Runat="server" />

        </asp:Panel>
</td></tr>
</table>
</form>
</body>
