<%@ Register TagPrefix="Title" TagName="TitleShow" Src="/aspnetdata/Title.ascx" %>
<%@ 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 * from Employees"
	sub Page_load(sender as object,e as EventArgs)
		Source = Ctype(Session("NowSTKSQL"),SqlConnection)
		if IsPostBack=false then
			bindNew(true)
		end if
	end sub
	sub bindNew(tag as Boolean)
		if tag=true then
			Dim DS as DataSet = new DataSet()
			Dim NComm as SqlDataAdapter = new SqlDataAdapter(d, Source)
			NComm.Fill(DS, "Employees")
			Emp.DataSource=DS.Tables("Employees").DefaultView
			Emp.DataBind()
			Session("NowDSSE")=DS'声明为Session
			Session("NowDSSEM")=NComm
		else
			Dim DS as DataSet = Ctype(Session("NowDSSE"),DataSet)
			Emp.DataSource=DS.Tables("Employees").DefaultView
			Emp.DataBind()
		end if
	end sub
	sub Emp_Edit(sender as object ,e as DataGridCommandEventArgs )
		Emp.EditItemIndex = e.Item.ItemIndex
		bindNew(false)
		Dim h as TextBox = Ctype(Emp.Items(e.Item.ItemIndex).Cells(1).Controls(1),TextBox)
		NowEmID.Text=h.Text
	end sub
	sub Emp_Cancel(sender as object, e as DataGridCommandEventArgs)
		Emp.EditItemIndex = -1 '取消
		bindNew(false)
	end sub
	sub Emp_Update(sender as object,e as DataGridCommandEventArgs)'保存
		PageVal.Visible=true
		if Page.IsValid=true then'若验证通过
			Dim DS as DataSet = Ctype(Session("NowDSSE"),DataSet)
			DIm NComm as SqlDataAdapter = Ctype(Session("NowDSSEM"),SqlDataAdapter)
			Dim Row as DataRow
			Row = DS.Tables("Employees").Rows(Emp.EditItemIndex)
			Dim i as integer=1
			for i=1 to 5
				Dim NowText as TextBox
				NowText = Ctype(Emp.Items(Emp.EditItemIndex).Cells(i).Controls(1),TextBox)
				Row(NowText.ID)=NowText.Text
			next
			Dim custCub as SqlCommandBuilder = new SqlCommandBuilder(NComm)
			NComm.Update(DS,"Employees")
			Emp.EditItemIndex = -1
			bindNew(true)
		end if
	end sub
	sub CheckEmID(source as object,args as ServerValidateEventArgs)
		PageVal.Visible=false'不显示整页验证
		try
			Dim DS as DataSet = Ctype(Session("NowDSSE"),DataSet)
			Dim h as string = "SELECT * from employees where EmID Not Like '" & NowEmID.Text & "'"
			Dim NComm as SqlDataAdapter = new SqlDataAdapter(h, Ctype(Session("NowSTKSQL"),SqlConnection))
			NComm.Fill(DS, "EmploySearch")
			DS.Tables("EmploySearch").DefaultView.RowFilter="EmID Like '" & args.Value & "'"
			Dim Row as DataRow
			for each Row in DS.Tables("EmploySearch").DefaultView
				return
			next
			args.IsValid = true'验证结果为真(不显示信息)
		catch
			args.IsValid = false'验证结果为伪(显示信息)
		end try
	end sub
	sub CheckPID(source as object,args as ServerValidateEventArgs)
		try
			dim iX1, iX2,iD1,iD2,iD3,iD4,iD5,iD6,iD7,iD8,iD9 as integer
			dim stCode as string
			dim iCode as integer = 0
			dim iASC as integer = 0
			dim p as string = args.Value
			dim p1 as string = Left(p,1)
			iASC = Asc(p1)
			if iASC>=65 and iASC<=72 then
				iCode = iASC - 55
			elseif iASC>=74 and iASC<=78 then
				iCode = iASC - 56
			elseif iASC>=80 and iASC<=86 then
				iCode = iASC - 57
			elseif iASC=87 then
				iCode = 32
			elseif iASC=88 then
				iCode = 30
			elseif iASC=89 then
				iCode = 31
			elseif iASC=90 then
				iCode = 33
			else
				args.IsValid = false
			end if
			stCode = iCode.ToString()
			iX1 = Convert.ToInt32(Left(stCode,1))
			iX2 = Convert.ToInt32(Right(stCode,1))
			iD1 = Convert.ToInt32(Mid(p,2,1))
			iD2 = Convert.ToInt32(Mid(p,3,1))
			iD3 = Convert.ToInt32(Mid(p,4,1))
			iD4 = Convert.ToInt32(Mid(p,5,1))
			iD5 = Convert.ToInt32(Mid(p,6,1))
			iD6 = Convert.ToInt32(Mid(p,7,1))
			iD7 = Convert.ToInt32(Mid(p,8,1))
			iD8 = Convert.ToInt32(Mid(p,9,1))
			iD9 = Convert.ToInt32(Mid(p,10,1))
			if (iX1 + 9 * iX2 + 8 * iD1 + 7 * iD2 + 6 * iD3 + 5 * iD4 + 4 * iD5 + 3 * iD6 + 2 * iD7 + iD8 + iD9) Mod 10 = 0 then
				args.IsValid = true
			else
				args.IsValid = false
			end if
		catch
			args.IsValid = false
		end try
	end sub
        </script>
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form runat="server">
<ASP:DataGrid id="Emp" runat="server"
        BorderColor="black"
        BorderWidth="1"
        GridLines="Both"
        CellPadding="3"
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="10pt"
        HeaderStyle-BackColor="GreenYellow"
AlternatingItemStyle-BackColor="LightYellow"
AutoGenerateColumns="False"
      OnEditCommand="Emp_Edit"
      OnCancelCommand="Emp_Cancel"
      OnUpdateCommand="Emp_Update"

>
      <Columns>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Save" ItemStyle-Wrap="false"/>
<asp:TemplateColumn HeaderText="人员编号" >
          <ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "EmID") %>' id="EmID1"/>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:Textbox runat="server" width="55" id="EmID" Text='<%# DataBinder.Eval(Container.DataItem, "EmID") %>'/>
    <asp:CustomValidator id="EmIDR" runat="server"
        ControlToValidate="EmID"
        OnServerValidate="CheckEmID"
        Display="Dynamic"
        Font-Name="verdana" Font-Size="10pt"
	>
           人员编输入错误，不可重复
    </asp:CustomValidator>
          </EditItemTemplate>
        </asp:TemplateColumn>
<asp:TemplateColumn HeaderText="姓名" >
          <ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Cname") %>' id="Cname1"/>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:Textbox runat="server" id="Cname" Text='<%# DataBinder.Eval(Container.DataItem, "Cname") %>'/>
        <asp:RequiredFieldValidator id="CnameR" ControlToValidate="Cname" Font-Name="Arial" Font-Size="11" runat="server">
            必须输入姓名
        </asp:RequiredFieldValidator>
          </EditItemTemplate>
        </asp:TemplateColumn>
<asp:TemplateColumn HeaderText="户籍地址" >
          <ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Address_hu") %>' id="Address_hu1"/>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:Textbox runat="server" id="Address_hu" Text='<%# DataBinder.Eval(Container.DataItem, "Address_hu") %>'/>
          </EditItemTemplate>
       </asp:TemplateColumn>
<asp:TemplateColumn HeaderText="身份证号" >
          <ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PID") %>' id="PID1"/>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:Textbox runat="server" id="PID" Text='<%# DataBinder.Eval(Container.DataItem, "PID") %>'/>
    <asp:CustomValidator id="PIDR" runat="server"
        ControlToValidate="PID"
        OnServerValidate="CheckPID"
        Display="Dynamic"
        Font-Name="verdana" Font-Size="10pt">
           身份证号输入错误
    </asp:CustomValidator>
          </EditItemTemplate>
        </asp:TemplateColumn>
<asp:TemplateColumn HeaderText="电子邮件" >
          <ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Email") %>' id="email1"/>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:Textbox runat="server" id="Email" Text='<%# DataBinder.Eval(Container.DataItem, "Email") %>'/>
          <asp:RegularExpressionValidator id="EmailR" runat="server"
              ControlToValidate="Email"
              ValidationExpression=".*@.*\..*"
              Display="Dynamic"
              Font-Name="verdana" 
              Font-Size="10pt">
                 电子邮件格式错误
          </asp:RegularExpressionValidator>
          </EditItemTemplate>
        </asp:TemplateColumn>

</Columns>
</ASP:DataGrid>
            <asp:ValidationSummary ID="PageVal" runat="server"
                HeaderText="无法保存，至少有一个栏未验证通过"
                Font-Name="verdana" 
                Font-Size="12" 
		ShowMessageBox="True"
                />
<asp:Label runat="server" Visible="False" id="NowEmID"/>
</form>
</body>