<%@ 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_Item(sender as Object,e as DataGridCommandEventArgs )
		if e.CommandName="Delete" then
			staffShow.SelectedIndex=e.Item.ItemIndex
		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 if
	end sub
	sub staffShow_Bound(sender as object,e as DataGridItemEventArgs )
		if e.Item.ItemIndex > -1 then
			dim k as Hyperlink = Ctype(e.Item.Cells(1).FindControl("DelBtn"),Hyperlink)
			dim x as string =e.Item.Cells(0).FindControl("DeleteRecord").UniqueID
			k.NavigateUrl="javascript:doconfirm('Are you sure?','" & x & "','')"
			k.Text="Delete"
		end if
	end sub

	</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="" 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>