<%@ 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="VB" runat="server">
	Dim Source as SqlConnection
	dim d as string="SELECT * from product"
	sub Page_load(sender as object,e as EventArgs)
		Source = Ctype(Session("NowSTKSQL"),SqlConnection)
		if IsPostBack=false then
			bindNew(true)
		end if
	end sub
	sub bindNew(tag as Boolean)'结合至DataGrid
		if tag=true then
			Dim DS as DataSet = new DataSet()
			Dim NComm as SqlDataAdapter = new SqlDataAdapter(d, Source)
			NComm.Fill(DS, "Product")
			Dim Pmykey(1) as DataColumn' = new DataColumn() '声明新主索引栏阵列
			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
			Dim DS as DataSet =Ctype(Session("NowDSP"),DataSet)
			AllProduct.DataSource=DS.Tables("Product").DefaultView
			AllProduct.DataBind()
		end if
	end sub
	sub AllProduct_Update(sender as object,e as DataListCommandEventArgs)
		Dim DS as DataSet = Ctype(Session("NowDSP"),DataSet)
		Dim NComm as SqlDataAdapter = Ctype(Session("NowComm"),SqlDataAdapter)
		Dim ModiRow as DataRow = Ctype(Session("NowRow"),DataRow)
		try
			ModiRow("ProductName")=Ctype(AllProduct.Items(AllProduct.EditItemIndex).FindControl("ProductName"),TextBox).Text
			ModiRow("PriceCus")=Ctype(AllProduct.Items(AllProduct.EditItemIndex).FindControl("PriceCus"),TextBox).Text
			ModiRow("Pages")=Ctype(AllProduct.Items(AllProduct.EditItemIndex).FindControl("Pages"),TextBox).Text
			ModiRow("StockCurrent")=Ctype(AllProduct.Items(AllProduct.EditItemIndex).FindControl("StockCurrent"),TextBox).Text
			Dim x as ASP.upload_ascx
			x=Ctype(AllProduct.Items(AllProduct.EditItemIndex).FindControl("PhotoU"),ASP.upload_ascx)
			ModiRow("Photo")=x.OKFile
			Dim custCB as SqlCommandBuilder = new SqlCommandBuilder(NComm)
			NComm.Update(DS,"Product")'保存
			AllProduct.EditItemIndex = -1
			bindNew(true)
		catch ex as Exception
			errMsg.Text="发生错误" + ex.ToString()
		end try
	end sub
	sub AllProduct_Cancel(sender as object,e as DataListCommandEventArgs)
		AllProduct.EditItemIndex=-1
		bindNew(false)
	end sub
	sub AllProduct_Edit(sender as object,e as DataListCommandEventArgs)
		AllProduct.SelectedIndex = e.Item.ItemIndex '设定选取记录
		AllProduct.EditItemIndex=e.Item.ItemIndex
		Dim DS as DataSet =Ctype(Session("NowDSP"),DataSet)
		Session("NowRow")=DS.Tables("Product").Rows(e.Item.ItemIndex)
		bindNew(false)
		Dim NowPath as string =Request.PhysicalPath'目前路径(含文件名)
		dim g as integer=NowPath.LastIndexOf("\")'取得最末的\符号
		NowPath=NowPath.Remove(g+1,12)'取得目前路径
		Dim y as ASP.upload_ascx
		y = Ctype(AllProduct.Items(AllProduct.EditItemIndex).FindControl("PhotoU"),ASP.upload_ascx)
		y.UpPath = NowPath
		y.ReplaceName = DS.Tables("Product").Rows(e.Item.ItemIndex)("Photo").ToString()
		y.Show()
	end sub
</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/vb/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>
