<%@ 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="VB" runat="server">
	dim Source as SqlConnection
	dim d as string ="SELECT Staff.*, Category.Content FROM Category INNER JOIN Staff ON Category.ID = Staff.Depart Order by SID"
	sub Page_load(sender as object ,e as EventArgs )
		Source = Ctype(Session("NowEmpSQL"),SqlConnection)
		if IsPostBack=false then
			bindNew(true)
		end if
	end sub
	sub bindNew(tag as Boolean) '结合至DataGrid
		if tag=true then
			Dim DS as DataSet = new DataSet()
			Dim NComm as SqlDataAdapter = new SqlDataAdapter(d, Source)
			NComm.Fill(DS, "staff")
			Session("NowDS")=DS'声明为Session
			staffShow.DataSource=DS.Tables("staff").DefaultView
			staffShow.DataBind()
		else
			Dim DS as DataSet=Ctype(Session("NowDS"),DataSet)
			staffShow.DataSource=DS.Tables("staff").DefaultView
			staffShow.DataBind()
		end if
	end sub
	sub staffShow_Del(sender as Object, e as DataGridCommandEventArgs)
		dim x as integer = staffShow.SelectedIndex '取得目前选取索引值
		if x<>-1 then '隐藏询问及打开Delete
			Ctype(staffShow.Items(x).Cells(0).FindControl("BtDelSure"),Label).Visible=false
			Ctype(staffShow.Items(x).Cells(0).FindControl("BtDelYes"),LinkButton).Visible=false
			Ctype(staffShow.Items(x).Cells(0).FindControl("BtDelNo"),LinkButton).Visible=false
			Ctype(staffShow.Items(x).Cells(0).FindControl("del"),LinkButton).Visible=true
		end if
		staffShow.SelectedIndex=e.Item.ItemIndex '设定选取
		Ctype(e.Item.Cells(0).FindControl("BtDelSure"),Label).Visible=true
		Ctype(e.Item.Cells(0).FindControl("BtDelYes"),LinkButton).Visible=true
		Ctype(e.Item.Cells(0).FindControl("BtDelNo"),LinkButton).Visible=true
		Ctype(e.Item.Cells(0).FindControl("del"),LinkButton).Visible=false
	end sub
	sub DelRecord(sender as Object ,e as EventArgs)
		dim Dstring as string
		dim Nowsid as string =staffShow.DataKeys(staffShow.SelectedIndex).ToString()
		Dstring = "Delete From Staff where SID ='" + Nowsid + "'"
		try
			Dim UpStaff as SqlCommand=new SqlCommand(Dstring,Source)
			UpStaff.Connection.Open()
			UpStaff.ExecuteNonQuery() '执行保存
			UpStaff.Connection.Close()
			bindNew(true)
			errMsg.Text="Delete OK"
		catch
			errMsg.Text="Delete fail"
		end try
	end sub
	sub DelRecordNo(sender as object,e as EventArgs)
		dim a as Label=Ctype(staffShow.Items(staffShow.SelectedIndex).Cells(0).FindControl("BtDelSure"),Label)
		dim b as LinkButton = Ctype(staffShow.Items(staffShow.SelectedIndex).Cells(0).FindControl("BtDelYes"),LinkButton)
		dim c as LinkButton = Ctype(staffShow.Items(staffShow.SelectedIndex).Cells(0).FindControl("BtDelNo"),LinkButton)
		dim d as LinkButton = Ctype(staffShow.Items(staffShow.SelectedIndex).Cells(0).FindControl("del"),LinkButton)
		a.Visible=false
		b.Visible=false
		c.Visible=false
		d.Visible=true
	end sub
	</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>