<%@ 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 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_Del(Object sender, DataGridCommandEventArgs e){
		int x = staffShow.SelectedIndex;//取得目前选取索引值
		if(x!=-1){//隐藏询问及打开Delete
			((Label)staffShow.Items[x].Cells[0].FindControl("BtDelSure")).Visible=false;
			((LinkButton)staffShow.Items[x].Cells[0].FindControl("BtDelYes")).Visible=false;
			((LinkButton)staffShow.Items[x].Cells[0].FindControl("BtDelNo")).Visible=false;
			((LinkButton)staffShow.Items[x].Cells[0].FindControl("del")).Visible=true;
		}
		staffShow.SelectedIndex=e.Item.ItemIndex;//设定选取
		((Label)e.Item.Cells[0].FindControl("BtDelSure")).Visible=true;
		((LinkButton)e.Item.Cells[0].FindControl("BtDelYes")).Visible=true;
		((LinkButton)e.Item.Cells[0].FindControl("BtDelNo")).Visible=true;
		((LinkButton)e.Item.Cells[0].FindControl("del")).Visible=false;
	}
	void DelRecord(Object sender,EventArgs e){
		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 DelRecordNo(object sender,EventArgs e){
		Label a =(Label)staffShow.Items[staffShow.SelectedIndex].Cells[0].FindControl("BtDelSure");
		LinkButton b = (LinkButton)staffShow.Items[staffShow.SelectedIndex].Cells[0].FindControl("BtDelYes");
		LinkButton c = (LinkButton)staffShow.Items[staffShow.SelectedIndex].Cells[0].FindControl("BtDelNo");
		LinkButton d = (LinkButton)staffShow.Items[staffShow.SelectedIndex].Cells[0].FindControl("del");
		a.Visible=false;
		b.Visible=false;
		c.Visible=false;
		d.Visible=true;
	}
	</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"
	OnDeleteCommand="staffShow_Del"
>
         <SelectedItemStyle BackColor="LightYellow">
         </SelectedItemStyle>
      <Columns>
	<asp:TemplateColumn>
	<itemtemplate >
	<asp:LinkButton runat="server" Text="Delete" id="del" CommandName="Delete" />
	<asp:label id="BtDelSure" Text="Sure?　" visible="false" Runat="server" />
	<asp:LinkButton id="BtDelYes" runat="server" Text="Yes　" visible="false" OnClick="DelRecord"/>
	<asp:LinkButton id="BtDelNo" runat="server"  Text="No" visible="false" OnClick="DelRecordNo"/>
	</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>