using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using WebLib; namespace Education.Forms.自行登錄 { public partial class 持有證照 : FormBase { private bool fileOK = false; private string 文件上傳目錄; protected void Page_Load(object sender, EventArgs e) { FunctionName = "持有證照登錄"; 文件上傳目錄 = Server.MapPath(PublicVariable.證照上傳路徑); if (!Directory.Exists(文件上傳目錄)) Directory.CreateDirectory(文件上傳目錄); Form_View = fv持有證照; Grid_View = gv持有證照; FormView_DataSource = ds持有證照Form; GridView_DataSource = ds持有證照Grid; AddTabButton(MultiView1, View處理, tab處理); AddTabButton(MultiView1, View查詢, tab查詢); if (IsPostBack) { Grid_View.DataBind(); Form_View.DataBind(); } else { ActivateTab(MultiView1, View處理); } } protected void btnSearch_Click(object sender, EventArgs e) { Grid_View.PageIndex = 0; Grid_View.DataBind(); if (Grid_View.Rows.Count > 0) { ActivateTab(MultiView1, View處理); } else { lbMessage.Text = "沒有資料,請以其他查詢條件查詢!"; } } protected void btnClear_Click(object sender, EventArgs e) { txt發證單位_Search.Text = ""; txt證照名稱_Search.Text = ""; txt證照字號_Search.Text = ""; } protected override void FormView_DataBinding(object sender, EventArgs e) { base.FormView_DataBinding(sender, e); RegisterPostbackTrigger(Form_View.FindControl("btnUpload")); } protected override void FormView_ReadOnlyDataBound(object sender, EventArgs e) { base.FormView_ReadOnlyDataBound(sender, e); RegisterPostbackTrigger(Form_View.FindControl("btnUpload")); if (Form_View.DataItem != null) { string 員工編號 = (Form_View.DataItem as Education.持有證照).員工編號; int 序號 = (Form_View.DataItem as Education.持有證照).序號; string tFullName = String.Format("{0}{1}_{2}.jpg", 文件上傳目錄, 員工編號, 序號); /* if (Form_View.FindControl("lt附件檔案") != null) { if (File.Exists(tFullName)) { FindControl(Form_View, "lt附件檔案").Text = String.Format("下載附件檔案", ResolveClientUrl(PublicVariable.證照上傳路徑), 員工編號, 序號); } else { FindControl(Form_View, "lt附件檔案").Text = "尚未上傳檔案"; } } */ Image image1 = FindControl(Form_View, "Image1"); if (image1 != null) { string[] tmp = image1.ImageUrl.Split('?'); if (tmp.Length > 0 && !File.Exists(Server.MapPath(tmp[0]))) { image1.ImageUrl = "~/picture/nopicture.jpg"; image1.Attributes.Remove("onclick"); image1.Style.Remove("cursor"); } else { image1.Attributes.Add("onclick", GetOpenWindowJS("../Common/ShowLargePicture.aspx", "_picture", "pic", String.Format("{0}{1}_{2}.jpg", Education.PublicVariable.證照上傳路徑, 員工編號, 序號))); image1.Style.Add("cursor", "pointer"); } } } } protected override void FormView_InsertDataBound(object sender, EventArgs e) { base.FormView_InsertDataBound(sender, e); FindControl(Form_View, "txt員工編號").Text = UserId; FindControl(Form_View, "txt員工編號X").Text = UserName; } protected override void FormView_EditDataBound(object sender, EventArgs e) { base.FormView_EditDataBound(sender, e); RadioButton rb1 = FindControl(Form_View, "rb證照名稱1"); RadioButton rb2 = FindControl(Form_View, "rb證照名稱2"); RadioButton rb3 = FindControl(Form_View, "rb證照名稱3"); RadioButton rb999 = FindControl(Form_View, "rb證照名稱999"); TextBox txt1 = FindControl(Form_View, "txt證照名稱"); if (txt1.Text.CompareTo(rb1.Text) == 0) { rb1.Checked = true; txt1.Text = ""; } else if (txt1.Text.CompareTo(rb2.Text) == 0) { rb2.Checked = true; txt1.Text = ""; } else if (txt1.Text.CompareTo(rb3.Text) == 0) { rb3.Checked = true; txt1.Text = ""; } else { rb999.Checked = true; } } protected override void FormView_ItemCommand(object sender, FormViewCommandEventArgs e) { base.FormView_ItemCommand(sender, e); string[] args = e.CommandArgument.ToString().Split('|'); string 員工編號 = String.Empty; int 序號 = -1; if (args.Length > 0) 員工編號 = args[0]; if (args.Length > 1) 序號 = DAC.GetInt32(args[1]); switch (e.CommandName) { case "上傳附件": if (員工編號 == String.Empty || 序號 == -1) return; FileUpload fuFile = FindControl(Form_View, "fu附件檔案"); 檢查上傳照片(fuFile); 儲存上傳文件(fuFile, 員工編號, 序號); Form_View.DataBind(); break; case "刪除附件": case "Delete": if (員工編號 == String.Empty || 序號 == -1) return; string fileName = String.Format("{0}{1}_{2}.jpg", 文件上傳目錄, 員工編號, 序號); if (File.Exists(fileName)) File.Delete(fileName); fileName = String.Format("{0}T_{1}_{2}.jpg", 文件上傳目錄, 員工編號, 序號); if (File.Exists(fileName)) File.Delete(fileName); Form_View.DataBind(); break; } } private void 檢查上傳照片(FileUpload fu) { // 處理上傳照片 string ext = ""; if (fu.HasFile && fu.PostedFile.ContentType.ToLower().IndexOf("image") >= 0) { // 檢查副檔名 ext = Path.GetExtension(fu.FileName).ToLower(); String[] allowedExt = { ".gif", ".jpeg", ".jpg", ".png" }; for (int i = 0; i < allowedExt.Length; i++) { if (ext == allowedExt[i]) { fileOK = true; } } // 檢查檔案大小 if (fileOK && fu.PostedFile.ContentLength < 5242880) fileOK = true; else fileOK = false; } else { fileOK = false; } } protected void 儲存上傳文件(FileUpload fu, string 員工編號, int 序號) { if (fileOK) { try { string tFullName = String.Format("{0}T_{1}_{2}.jpg", 文件上傳目錄, 員工編號, 序號); MemoryStream ms = PictureProcess.縮圖(fu.PostedFile.InputStream, 300, 300); File.WriteAllBytes(tFullName, ms.ToArray()); tFullName = String.Format("{0}{1}_{2}.jpg", 文件上傳目錄, 員工編號, 序號); ms = PictureProcess.縮圖(fu.PostedFile.InputStream, 2000, 2000); File.WriteAllBytes(tFullName, ms.ToArray()); ms.Dispose(); } catch { throw; } } } protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { args.IsValid = false; Control container = (source as Control).Parent; RadioButton rb1 = FindControl(container, "rb證照名稱1"); RadioButton rb2 = FindControl(container, "rb證照名稱2"); RadioButton rb3 = FindControl(container, "rb證照名稱3"); RadioButton rb999 = FindControl(container, "rb證照名稱999"); TextBox txt1 = FindControl(container, "txt證照名稱"); if (rb1.Checked) { txt1.Text = rb1.Text; args.IsValid = true; } else if (rb2.Checked) { txt1.Text = rb2.Text; args.IsValid = true; } else if (rb3.Checked) { txt1.Text = rb3.Text; args.IsValid = true; } else if (rb999.Checked) { args.IsValid = txt1.Text.Trim().Length > 0; } } protected void ds持有證照Grid_Selecting(object sender, ObjectDataSourceSelectingEventArgs e) { e.InputParameters["員工編號"] = UserId; } } }