<%@ 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" %>
<%@Import Namespace="System.Threading"%>
<%@Import Namespace="System.Resources"%>
<%@Import Namespace="System.Globalization"%>
<html>
<head>
	<script language="C#" runat="server">
	SqlConnection Source;
	string d="SELECT Staff.*, Category.Content FROM Category INNER JOIN Staff ON Category.ID = Staff.Depart Order by SID";
	void Page_load(object sender,EventArgs e){
		Source = (SqlConnection)Session["NowEmpSQL"];
		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_Item(Object sender, DataGridCommandEventArgs e){
		if(e.CommandName=="Delete"){
			staffShow.SelectedIndex=e.Item.ItemIndex;
			string Dstring;
			string Nowsid=staffShow.DataKeys[staffShow.SelectedIndex].ToString();		
			Dstring = "Delete From Staff where SID ='" + Nowsid + "'";
			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";
			}
		}
	}
	void staffShow_Bound(object sender,DataGridItemEventArgs e){
		if(e.Item.ItemIndex > -1){
			HyperLink k=(HyperLink)e.Item.Cells[1].FindControl("DelBtn");
			string x=e.Item.Cells[0].FindControl("DeleteRecord").UniqueID;
			k.NavigateUrl="javascript:doconfirm('Are you sure?','DelR','')";
			k.Text="Delete";
		}
	}
	</script>
</head>
<script language="javascript">
<!--
function doconfirm(ask,arg1,arg2) {
	if(confirm(ask)) __doPostBack(arg1,arg2);
}
// -->
</script>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form id="FormTask" runat="server" enctype="multipart/form-data">
<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"
	SelectedItemStyle-BackColor="#FFFFC0"
	OnItemCommand="staffShow_Item"
	OnItemDataBound="staffShow_Bound" 
>
       <Columns>
	<asp:TemplateColumn>
	<itemtemplate >
	<asp:Hyperlink id="DelBtn" Runat="server" />
	<asp:LinkButton id="DeleteRecord" runat="server" Visible="true" text="del" CommandName="Delete" />
	</ItemTEMPLATE>
	</asp:TemplateColumn>
	<asp:BoundColumn HeaderText="SID" DataField="SID" ItemStyle-Wrap="false"/>
	<asp:BoundColumn HeaderText="Name" DataField="cname"/>
	<asp:BoundColumn HeaderText="PID" DataField="pid"/>
	<asp:TemplateColumn HeaderText="Birthday" >
		<ItemTemplate>
			<asp:Label runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "Bday","{0:yyyy/MM/dd}") %> id="Bday"/>
		</ItemTemplate>
	</asp:TemplateColumn>
	<asp:TemplateColumn HeaderText="Department" >
		<ItemTemplate>
			<asp:Label runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "Content") %> id="Content"/>
		</ItemTemplate>
	</asp:TemplateColumn>
	</Columns>
</ASP:DataGrid>
</form>