<%@ 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 DeleteSelect(Object sender, EventArgs e){
		string DelString,SIDString="";
		for(int i=0;i<staffShow.Items.Count;i++){
			CheckBox h;
			h=(CheckBox)staffShow.Items[i].Cells[0].Controls[1];
			if(h.Checked == true){ //取得已选取的主键
				SIDString +=" or (SID ='" + staffShow.Items[i].Cells[1].Text + "')";
			}
		}
		if(SIDString.Length>0){
			SIDString=SIDString.Remove(0,4);
			DelString = "Delete from Staff where " + SIDString;
			SqlCommand DelRec = new SqlCommand(DelString,Source);
			DelRec.Connection.Open();
			DelRec.ExecuteNonQuery();
			DelRec.Connection.Close();
			bindNew(true);
			errMsg.Text="Delete OK.";
		}
	}
	</script>
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form runat="server" id="test">
<asp:button id="delbtn" onclick="DeleteSelect" text="Delete Selected records" 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"
	DataKeyField="SID"
	AutoGenerateColumns="FALSE"
        HeaderStyle-BackColor="LightYellow"
	HeaderStyle-HorizontalAlign="Center"
	SelectedItemStyle-BackColor="#FFFFC0"
>
       <Columns>
<asp:TemplateColumn HeaderText="Delete" ItemStyle-HorizontalAlign="Center">
          <ItemTemplate>
<asp:CheckBox runat="server" ID="del" />
          </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>