<%@ 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 Customers"
	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)
		if tag=true then
			DIM DS as DataSet = new DataSet()
			Dim NComm as SqlDataAdapter = new SqlDataAdapter(d, Source)
			NComm.Fill(DS, "Customers")
			Session("NowDSE")=DS'声明为Session
			AllCustomer.DataSource=DS.Tables("Customers").DefaultView
			AllCustomer.DataBind()
		else
			Dim DS as DataSet =Ctype(Session("NowDSE"),DataSet)
			AllCustomer.DataSource=DS.Tables("Customers").DefaultView
			AllCustomer.DataBind()
		end if
	end sub
	sub AllCustomer_EditCommand(sender as object,e as DataListCommandEventArgs)
		AllCustomer.EditItemIndex = e.Item.ItemIndex
		bindNew(false)
	end sub
	sub AllCustomer_CancelCommand(sender as object,e as  DataListCommandEventArgs)
		AllCustomer.EditItemIndex = -1
		bindNew(false)
	end sub
	sub AllCustomer_UpdateCommand(sender as object,e as DataListCommandEventArgs)
		Dim DS as DataSet = Ctype(Session("NowDSE"),DataSet)
		Dim CusIDLabel as Label  = Ctype(e.Item.FindControl("CusID"),Label)
		Dim w as string =" where CusID ='" & CusIDLabel.Text & "'"
		Dim NowComm as SqlDataAdapter = new SqlDataAdapter(d + w, Source)
		NowComm.Fill(DS,"ModiCus")
		Dim CusLongNameText as TextBox = Ctype(e.Item.FindControl("CusLongName"),TextBox)
		Dim CusAddressText as TextBox = Ctype(e.Item.FindControl("CusAddress"),TextBox)
		Dim DeliAddressText as TextBox  = Ctype(e.Item.FindControl("DeliAddress"),TextBox)
		Dim InvoNumText as TextBox = Ctype(e.Item.FindControl("InvoNum"),TextBox)
		Dim TEL1Text as TextBox = Ctype(e.Item.FindControl("TEL1"),TextBox)
		Dim FAX1Text as TextBox = Ctype(e.Item.FindControl("FAX1"),TextBox)
		DS.Tables("ModiCus").Rows(0)("CusLongName")=CusLongNameText.Text
		DS.Tables("ModiCus").Rows(0)("CusAddress")=CusAddressText.Text
		DS.Tables("ModiCus").Rows(0)("DeliAddress")=DeliAddressText.Text
		DS.Tables("ModiCus").Rows(0)("InvoNum")=InvoNumText.Text
		DS.Tables("ModiCus").Rows(0)("TEL1")=TEL1Text.Text
		DS.Tables("ModiCus").Rows(0)("FAX1")=FAX1Text.Text
		Dim custCB as SqlCommandBuilder = new SqlCommandBuilder(NowComm)
		NowComm.Update(DS,"ModiCus")'保存
		AllCustomer.EditItemIndex = -1
		bindNew(true)
	end sub
	sub AllCustomer_DeleteCommand(sender as object,e as DataListCommandEventArgs)
		dim DString as string
		Dim Nowid as string=AllCustomer.DataKeys(e.Item.ItemIndex).ToString()
		Dstring = "Delete From Customers where CusID ='" & Nowid & "'"
		try
			Dim UpStaff as SqlCommand =new SqlCommand(Dstring,Source)
			UpStaff.Connection.Open()
			UpStaff.ExecuteNonQuery() '执行保存
			UpStaff.Connection.Close()
			bindNew(true)
			errMsg.Text="Delete OK"
		catch
			errMsg.Text="Delete fail"
		end try
		bindNew(true)
	end sub
        ReadOnly Property TotalCus() As String
            Get
		Dim DS as DataSet = Ctype(Session("NowDSE"),DataSet)
		dim x as integer =DS.Tables("Customers").Rows.Count
		return x.ToString()
            End Get
        End Property
	</script>
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form runat="server">
<asp:label id="errMsg" runat="server" />
<asp:DataList id="AllCustomer" runat="server"
	BorderColor="black"	BorderWidth="1"
	GridLines="Both"	CellPadding="3"
	CellSpacing="0" 	Font-Name="Verdana"
	Font-Size="8pt" 	RepeatColumns="4"
	Width="650px"		HeaderStyle-BackColor="#C0FFFF"
	AlternatingItemStyle-BackColor="Gainsboro"
	EditItemStyle-BackColor="#FFFFC0"
	dataKeyField="CusID"
	ShowFooter="true"
	OnEditCommand="AllCustomer_EditCommand"
	OnUpdateCommand="AllCustomer_UpdateCommand"
	OnCancelCommand="AllCustomer_CancelCommand"
	OnDeleteCommand="AllCustomer_DeleteCommand"
            >
	<HeaderTemplate>
                客户基本资料
	</HeaderTemplate>
	<ItemTemplate>
		<asp:LinkButton id="button1" runat="server" Text="Edit" CommandName="edit" />
		<asp:LinkButton id="button2" runat="server" Text="Delete" CommandName="delete" /><br>
		客户编号：<%# DataBinder.Eval(Container.DataItem,"CusID")  %><br>
		公司名称：<%# DataBinder.Eval(Container.DataItem,"CusLongName")  %><br>
		连络人：<%# DataBinder.Eval(Container.DataItem,"Contact")  %><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="CusID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CusID")  %>' /><br>
		公司名称：
		<asp:TextBox id="CusLongName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CusLongName")  %>' /><br>
		公司地址：
		<asp:TextBox id="CusAddress" Rows="2" TextMode="MultiLine" Width="200" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CusAddress")  %>' /><br>
		送货地址：
		<asp:TextBox id="DeliAddress" Rows="2" TextMode="MultiLine" Width="200" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"DeliAddress")  %>' /><br>
		统一编号：
		<asp:TextBox id="InvoNum" Width="60" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"InvoNum")  %>' /><br>
		电话：
		<asp:TextBox id="TEL1" Width="70" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"TEL1")  %>' />
		传真：
		<asp:TextBox id="FAX1" Width="70" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FAX1")  %>' />
	</EditItemTemplate>
	<FooterTemplate>
                客户总数：<%# TotalCus %>
	</FooterTemplate>
        </asp:DataList>
</form>
</body>
