<%@ Register TagPrefix="Title" TagName="TitleShow" Src="/aspnetdata/Title.ascx" %>
<%@ Register TagPrefix="Up" TagName="Upload" Src="Upload.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);
			}
	}
	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["NowDSP"]=DS;//声明为Session
			Session["NowComm"]=NComm;
			AllProduct.DataSource=DS.Tables["Product"].DefaultView;
			AllProduct.DataBind();
		}
		else{
			DataSet DS=(DataSet)Session["NowDSP"];
			AllProduct.DataSource=DS.Tables["Product"].DefaultView;
			AllProduct.DataBind();
		}
	}
	void AllProduct_Update(object sender,DataListCommandEventArgs e){
		DataSet DS = (DataSet)Session["NowDSP"];
		SqlDataAdapter NComm = (SqlDataAdapter)Session["NowComm"];
		DataRow ModiRow = (DataRow)Session["NowRow"];
		try{
			ModiRow["ProductName"]=((TextBox)AllProduct.Items[AllProduct.EditItemIndex].FindControl("ProductName")).Text;
			ModiRow["PriceCus"]=((TextBox)AllProduct.Items[AllProduct.EditItemIndex].FindControl("PriceCus")).Text;
			ModiRow["Pages"]=((TextBox)AllProduct.Items[AllProduct.EditItemIndex].FindControl("Pages")).Text;
			ModiRow["StockCurrent"]=((TextBox)AllProduct.Items[AllProduct.EditItemIndex].FindControl("StockCurrent")).Text;
			ASP.upload_ascx x;
			x=(ASP.upload_ascx)AllProduct.Items[AllProduct.EditItemIndex].FindControl("PhotoU");
			ModiRow["Photo"]=x.OKFile;
			SqlCommandBuilder custCB = new SqlCommandBuilder(NComm);
			NComm.Update(DS,"Product");//保存
			AllProduct.EditItemIndex = -1;
			bindNew(true);
		}
		catch(Exception ex){
			errMsg.Text="发生错误" + ex.ToString();
		}
	}
	void AllProduct_Cancel(object sender,DataListCommandEventArgs e){
		AllProduct.EditItemIndex=-1;
		bindNew(false);
	}
	void AllProduct_Edit(object sender,DataListCommandEventArgs e){
		AllProduct.SelectedIndex = e.Item.ItemIndex; //设定选取记录
		AllProduct.EditItemIndex=e.Item.ItemIndex;
		DataSet DS=(DataSet)Session["NowDSP"];
		Session["NowRow"]=DS.Tables["Product"].Rows[e.Item.ItemIndex];
		bindNew(false);
		string NowPath=Request.PhysicalPath;//目前路径(含文件名)
		int g=NowPath.LastIndexOf("\\");//取得最末的\符号
		NowPath=NowPath.Remove(g+1,12);//取得目前路径
		ASP.upload_ascx y;
		y = (ASP.upload_ascx)AllProduct.Items[AllProduct.EditItemIndex].FindControl("PhotoU");
		y.UpPath = NowPath;
		y.ReplaceName = DS.Tables["Product"].Rows[e.Item.ItemIndex]["Photo"].ToString();
		y.Show();
	}
</script>
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form enctype="multipart/form-data" runat="server">
查看<asp:hyperlink id="h1" NavigateUrl="/aspnetdata/shows.aspx?cate=c&file=ch12/12-2/cs/upload.ascx" Text="upload.ascx" runat="server" target="_blank"/>、
<asp:label id="errMsg" runat="server" />
<asp:DataList id="AllProduct" runat="server"
	BorderColor="black"	BorderWidth="1"
	GridLines="Both"	CellPadding="3"
	CellSpacing="0" 	Font-Name="Verdana"
	Font-Size="10pt" 	RepeatColumns="3"
	Width="500px"		HeaderStyle-BackColor="#C0FFFF"
	AlternatingItemStyle-BackColor="Gainsboro"
	RepeatDirection="Horizontal"
	SelectedItemStyle-BackColor="#FFFFC0"
	DataKeyField="ProductID"
	OnEditCommand="AllProduct_Edit"
	OnUpdateCommand="AllProduct_Update"
	OnCancelCommand="AllProduct_Cancel"
            >
<ItemStyle Width="180px"></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>
<EditItemTemplate>
	<asp:LinkButton id="button2" runat="server" Text="Save" CommandName="Update" />
	<asp:LinkButton id="button3" runat="server" Text="Cancel" CommandName="Cancel" /><br>
书籍编号：<asp:Label ID="ProductID" Text='<%# DataBinder.Eval(Container.DataItem,"ProductID") %>' Runat="server" /><br>
书名：<asp:textbox ID="ProductName" Text='<%# DataBinder.Eval(Container.DataItem,"ProductName") %>' Width="160" Runat="server" /><br>
页数：<asp:textbox ID="Pages" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Pages") %>' Width="50"  /><br>
建议售价：<asp:textbox ID="PriceCus" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"PriceCus") %>' Width="50"/><br>
目前库存：<asp:textbox Width="50" ID="StockCurrent" Text='<%# DataBinder.Eval(Container.DataItem,"StockCurrent") %>' Runat="server" /><br>
<Up:Upload runat="server" id="PhotoU" ReplaceName='<%# DataBinder.Eval(Container.DataItem,"Photo") %>'/>
<br><asp:image id="Photo" width="80" Height="105"  Runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"Photo") %>' />
</EditItemTemplate>
        </asp:DataList>

</form>
</body>
