<%@ 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="C#" runat="server">
	SqlConnection Source;
	string d="SELECT * from Employees";
	void Page_load(object sender,EventArgs e){
		Source = (SqlConnection)Session["NowSTKSQL"];
		if(IsPostBack==false){
			bindNew(true);
		}
	}
	void bindNew(Boolean tag){
		if(tag==true){
			DataSet DS = new DataSet();
			SqlDataAdapter NComm = new SqlDataAdapter(d, Source);
			NComm.Fill(DS, "Employees");
			Emp.DataSource=DS.Tables["Employees"].DefaultView;
			Emp.DataBind();
			Session["NowDSSE"]=DS;//声明为Session
			Session["NowDSSEM"]=NComm;
		}
		else{
			DataSet DS = (DataSet)Session["NowDSSE"];
			Emp.DataSource=DS.Tables["Employees"].DefaultView;
			Emp.DataBind();
		}
	}
	void Emp_Edit(object Sender , DataGridCommandEventArgs e){
		Emp.EditItemIndex = e.Item.ItemIndex;
		bindNew(false);
		TextBox h=(TextBox)Emp.Items[e.Item.ItemIndex].Cells[1].Controls[1];
		NowEmID.Text=h.Text;
	}
	void Emp_Cancel(object Sender, DataGridCommandEventArgs e){
		Emp.EditItemIndex = -1; //取消
		bindNew(false);
	}
	void Emp_Update(object Sender,DataGridCommandEventArgs e){//保存
		PageVal.Visible=true;
		if(Page.IsValid==true){//若验证通过
			DataSet DS = (DataSet)Session["NowDSSE"];
			SqlDataAdapter NComm = (SqlDataAdapter)Session["NowDSSEM"];
			DataRow Row;
			Row = DS.Tables["Employees"].Rows[Emp.EditItemIndex];
			for(int i=1;i<6;i++){
				TextBox NowText;
				NowText = (TextBox)Emp.Items[Emp.EditItemIndex].Cells[i].Controls[1];
				Row[NowText.ID]=NowText.Text;
			}
			SqlCommandBuilder custCB = new SqlCommandBuilder(NComm);
			NComm.Update(DS,"Employees");
			Emp.EditItemIndex = -1;
			bindNew(true);
		}
	}
	void CheckEmID(object source, ServerValidateEventArgs args){
		PageVal.Visible=false;//不显示整页验证
		try{
			DataSet DS = (DataSet)Session["NowDSSE"];
			string h = "SELECT * from employees where EmID Not Like '" + NowEmID.Text + "'";
			SqlDataAdapter NComm = new SqlDataAdapter(h, Source);
			NComm.Fill(DS, "EmploySearch");
			DS.Tables["EmploySearch"].DefaultView.RowFilter="EmID Like '" + args.Value + "'";
			foreach(DataRow Row in DS.Tables["EmploySearch"].DefaultView){
				return;
			}
			args.IsValid = true;//验证结果为真(不显示信息)
		}
		catch{
			args.IsValid = false;//验证结果为伪(显示信息)
		}
	}
	void CheckPID(object source,ServerValidateEventArgs args){
		try{
			int iX1, iX2,iD1,iD2,iD3,iD4,iD5,iD6,iD7,iD8,iD9;
			string stCode;
			int iCode=0, iASC=0;
			string p = args.Value;
			string p1 = p[0].ToString();
			string AllEng="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			iASC = AllEng.IndexOf(p1)+65;
			if(iASC>=65 && iASC<=72){
				iCode = iASC - 55;}
			else if(iASC>=74 && iASC<=78){
				iCode = iASC - 56;}
			else if(iASC>=80 && iASC<=86){
				iCode = iASC - 57;}
			else if(iASC==87){
				iCode = 32;}
			else if(iASC==88){
				iCode = 30;}
			else if(iASC==89){
				iCode = 31;}
			else if(iASC==90){
				iCode = 33;}
			else{
				args.IsValid = false;
			}
			stCode = iCode.ToString();
			iX1 = Convert.ToInt32(stCode[0].ToString());
			iX2 = Convert.ToInt32(stCode[1].ToString());
			iD1 = Convert.ToInt32(p[1].ToString());
			iD2 = Convert.ToInt32(p[2].ToString());
			iD3 = Convert.ToInt32(p[3].ToString());
			iD4 = Convert.ToInt32(p[4].ToString());
			iD5 = Convert.ToInt32(p[5].ToString());
			iD6 = Convert.ToInt32(p[6].ToString());
			iD7 = Convert.ToInt32(p[7].ToString());
			iD8 = Convert.ToInt32(p[8].ToString());
			iD9 = Convert.ToInt32(p[9].ToString());
			if((iX1 + 9 * iX2 + 8 * iD1 + 7 * iD2 + 6 * iD3 + 5 * iD4 + 4 * iD5 + 3 * iD6 + 2 * iD7 + iD8 + iD9) % 10 == 0){
				args.IsValid = true;
			}
			else{
				args.IsValid = false;
			}
		}
		catch{
			args.IsValid = false;
		}
	}
        </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>