<%@ 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 Customers";
	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, "Customers");
			Session["NowDSE"]=DS;//声明为Session
			AllCustomer.DataSource=DS.Tables["Customers"].DefaultView;
			AllCustomer.DataBind();
		}
		else{
			DataSet DS=(DataSet)Session["NowDSE"];
			AllCustomer.DataSource=DS.Tables["Customers"].DefaultView;
			AllCustomer.DataBind();
		}
	}
	void AllCustomer_EditCommand(object sender,DataListCommandEventArgs e){
		AllCustomer.EditItemIndex = e.Item.ItemIndex;
		bindNew(false);
	}
	void AllCustomer_CancelCommand(object sender, DataListCommandEventArgs e){
		AllCustomer.EditItemIndex = -1;
		bindNew(false);
	}
	void AllCustomer_UpdateCommand(object sender,DataListCommandEventArgs e){
		DataSet DS = (DataSet)Session["NowDSE"];
		Label CusIDLabel = (Label)e.Item.FindControl("CusID");
		string w=" where CusID ='" + CusIDLabel.Text + "'";
		SqlDataAdapter NowComm = new SqlDataAdapter(d + w, Source);
		NowComm.Fill(DS,"ModiCus");
		TextBox CusLongNameText = (TextBox)e.Item.FindControl("CusLongName");
		TextBox CusAddressText = (TextBox)e.Item.FindControl("CusAddress");
		TextBox DeliAddressText = (TextBox)e.Item.FindControl("DeliAddress");
		TextBox InvoNumText = (TextBox)e.Item.FindControl("InvoNum");
		TextBox TEL1Text = (TextBox)e.Item.FindControl("TEL1");
		TextBox FAX1Text = (TextBox)e.Item.FindControl("FAX1");
		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;
		SqlCommandBuilder custCB = new SqlCommandBuilder(NowComm);
		NowComm.Update(DS,"ModiCus");//保存
		AllCustomer.EditItemIndex = -1;
		bindNew(true);
	}
	void AllCustomer_DeleteCommand(object sender,DataListCommandEventArgs e){
		string Dstring;
		string Nowid=AllCustomer.DataKeys[e.Item.ItemIndex].ToString();		
		Dstring = "Delete From Customers where CusID ='" + Nowid + "'";
		try{
			SqlCommand UpStaff=new SqlCommand(Dstring,Source);
			UpStaff.Connection.Open();
			UpStaff.ExecuteNonQuery();//执行保存
			UpStaff.Connection.Close();
			bindNew(true);
			errMsg.Text="Delete OK";
		}
		catch{
			errMsg.Text="Delete fail";
		}
		bindNew(true);
	}
	string TotalCus{  
		get{
			DataSet DS = (DataSet)Session["NowDSE"];
			int x=DS.Tables["Customers"].Rows.Count;
			return x.ToString();
		} 
	}
	</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>
