<%@ 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 SID,cname,pid,sex,Bday from staff";
	void Page_load(object sender,EventArgs e){
	Source = (SqlConnection)Session["NowEmpSQL"];
	errMsg.Text="";
	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, "staff");
		Session["NowDS"]=DS;//声明为Session
		staffShow.DataSource=DS.Tables["staff"].DefaultView;
		staffShow.DataBind();
	}
	else{
		DataSet DS=(DataSet)Session["NowDS"];
		staffShow.DataSource=DS.Tables["staff"].DefaultView;
		staffShow.DataBind();
	}
	}
	void staffShow_Edit(object Sender,DataGridCommandEventArgs e){
	staffShow.EditItemIndex = e.Item.ItemIndex;//编辑
	bindNew(false);
	}
	void staffShow_Cancel(object Sender,DataGridCommandEventArgs e){
	staffShow.EditItemIndex = -1; //取消
	bindNew(false);
	}
	void staffShow_Update(object Sender,DataGridCommandEventArgs e){
	string Nowsid=staffShow.DataKeys[staffShow.EditItemIndex].ToString();
	string Ustring; //更新的T-SQL
	Ustring="Update Staff Set";
	Ustring+=" cname='" + ((TextBox)e.Item.Cells[2].Controls[0]).Text + "'";
	Ustring+=", pid ='"+ ((TextBox)e.Item.Cells[3].Controls[0]).Text + "'";
	string Inputdate=((TextBox)e.Item.Cells[4].Controls[0]).Text;
	try{
		if(Inputdate.Length>0){
			DateTime b=Convert.ToDateTime(((TextBox)e.Item.Cells[4].Controls[0]).Text);
			Ustring+=", Bday ='" + b.ToString("yyyy/MM/dd") + "'";
		}
		Ustring+=" where SID ='" + Nowsid + "'";
		SqlCommand UpStaff=new SqlCommand(Ustring,Source);
		UpStaff.Connection.Open();
		UpStaff.ExecuteNonQuery();//执行保存
		UpStaff.Connection.Close();
		staffShow.EditItemIndex = -1;
		bindNew(true);
	}
	catch(Exception ex){
		errMsg.Text="发生错误，无法保存";
	}
	}
	</script>
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form runat="server">
<asp:label id="errMsg" Runat="server" />
      <ASP:DataGrid id="staffShow" runat="server"
        BorderColor="black"
        BorderWidth="1"
        GridLines="Both"
        CellPadding="3"
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="9pt"
        HeaderStyle-BackColor="#aaaadd"
	DataKeyField="SID"
	aUTOgENERATEcOLUMNS="FALSE"
	OnEditCommand="staffShow_Edit"
	OnCancelCommand="staffShow_Cancel"
	OnUpdateCommand="staffShow_Update">
      <Columns>
        <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Save" ItemStyle-Wrap="false"/>
	<asp:BoundColumn HeaderText="SID" ReadOnly="True" DataField="SID" ItemStyle-Wrap="false"/>
	<asp:BoundColumn HeaderText="Name" DataField="cname"/>
	<asp:BoundColumn HeaderText="PID" DataField="pid"/>
	<asp:BoundColumn HeaderText="Birthday" DataField="Bday" DataFormatString="{0:yyyy/MM/dd}" />
      </Columns>
</ASP:DataGrid>
</form>