<%@ 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 SID,cname,pid,sex,Bday from staff"
	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_Edit(sender as object ,e as DataGridCommandEventArgs)
	staffShow.EditItemIndex = e.Item.ItemIndex '编辑
	bindNew(false)
	end sub
	sub staffShow_Cancel(sender as object, e as DataGridCommandEventArgs)
	staffShow.EditItemIndex = -1  '取消
	bindNew(false)
	end sub
	sub staffShow_Update(sender as object ,e as DataGridCommandEventArgs)
	dim Nowsid as string =staffShow.DataKeys(staffShow.EditItemIndex).ToString()
	dim Ustring as string '更新的T-SQL
	Ustring="Update Staff Set"
	Ustring +=" cname ='" & Ctype(e.Item.Cells(2).Controls(0),TextBox).Text & "'"
	Ustring +=", pid ='" & CType(e.Item.Cells(3).Controls(0),TextBox).Text & "'"
	dim Inputdate as string =CType(e.Item.Cells(4).Controls(0),TextBox).Text
	try
		if Inputdate<>"" then
			dim b as DateTime=Convert.ToDateTime(Ctype(e.Item.Cells(4).Controls(0),TextBox).Text)
			Ustring +=", Bday ='" & b.ToString("yyyy/MM/dd") & "'"
		end if
		Ustring +=" where SID ='" & Nowsid & "'"
		Dim Upstaff as SqlCommand=new SqlCommand(Ustring,Source)
		UpStaff.Connection.Open()
		UpStaff.ExecuteNonQuery() '执行保存
		UpStaff.Connection.Close()
		staffShow.EditItemIndex = -1
		bindNew(true)
	catch ex as Exception
		errMsg.Text="发生错误，无法保存"
	end try
	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"
	OnEditCommand="staffShow_Edit"
	OnCancelCommand="staffShow_Cancel"
	OnUpdateCommand="staffShow_Update">
      <Columns>
        <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Save" ItemStyle-Wrap="false"/>
	<asp:BoundColumn HeaderText="SID" ReadOnly="True" DataField="SID" ItemStyle-Wrap="false"/>
	<asp:BoundColumn HeaderText="Name" DataField="cname"/>
	<asp:BoundColumn HeaderText="PID" DataField="pid"/>
	<asp:BoundColumn HeaderText="Birthday" DataField="Bday" DataFormatString="{0:yyyy/MM/dd}" />
      </Columns>
</ASP:DataGrid>
</form>