<%@ 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"
	sub Page_load(sender as object, E as eventArgs)
		Source = cTYPE(Session("NowSTKSQL"),SqlConnection)
		if IsPostBack=false then
			bindNew(true)
			Panel1.Visible=false
		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")
			Dim PmyKey(1) as DataColumn
			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
			Dim DS as DataSet =Ctype(Session("NowDSE"),DataSet)
			AllProduct.DataSource=DS.Tables("Product").DefaultView
			AllProduct.DataBind()
		end if
	end sub
	sub AllProduct_Edit(sender as object,e as DataListCommandEventArgs )
		AllProduct.SelectedIndex = e.Item.ItemIndex '设定选取记录
		Panel1.Visible=true '打开Panel
		Dim DS as DataSet = Ctype(Session("NowDSE"),DataSet)
		Dim x as Label = Ctype(e.Item.FindControl("PID"),Label)
		Dim fRow as DataRow = 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
	end sub
	sub CelClick(sender as object ,e as EventArgs)
		AllProduct.SelectedIndex=-1
		Panel1.Visible=false
	end sub
	sub OKClick(sender as object ,e as EventArgs)
		Dim DS as DataSet = Ctype(Session("NowDSE"),DataSet)
		Dim NComm as SqlDataAdapter = Ctype(Session("NowComm"),SqlDataAdapter)
		Dim ModiRow as DataRow = Ctype(Session("NowRow"),DataRow)
		try
			ModiRow("ProductName")=ProductName.Text
			ModiRow("PriceCus")=PriceCus.Text
			ModiRow("Pages")=Pages.Text
			ModiRow("StockCurrent")=StockCurrent.Text
			ModiRow("Photo")=Photo.ImageUrl
			Dim custCB as SqlCommandBuilder = new SqlCommandBuilder(NComm)
			NComm.Update(DS,"Product") '保存
			AllProduct.SelectedIndex = -1
			bindNew(true)
			Panel1.Visible=false
		catch ex as Exception
			errMsg.Text="发生错误" & ex.ToString()
		end try
	end sub
	sub ExecUpload(sender as Object,e as EventArgs)
		try
		if Not IsNothing(File1.PostedFile) then
			dim a as integer = File1.PostedFile.FileName.IndexOf(":")'取得位置
			dim x as string =File1.PostedFile.FileName.Remove(0,a+2)'截字串
			dim y as string =Request.PhysicalPath.Remove(Request.PhysicalPath.Length-11,11)
			File1.PostedFile.SaveAs(y & x)
			Photo.ImageUrl=x
		end if
		catch
		end try
	end sub
	sub ClearPho(sender as Object,e as EventArgs)
		Photo.ImageUrl=""
	end sub
	</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>
