// Ràng buộc + thêm xóa sửa protected void gvAdmin_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "EditRow") { int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex; gvAdmin.EditIndex = rowIndex; BindDataGridView(); } else if (e.CommandName == "DeleteRow") { MstrDet mstrDetDel = mstrDet_bus.GetAll().FirstOrDefault(m => m.MstrId == System.Convert.ToInt32(e.CommandArgument)); Mstr mstrDel = mstr.GetAll().FirstOrDefault(m => m.MstrId == System.Convert.ToInt32(e.CommandArgument)); Usr usrDel = (from c in usr.GetAll() where c.UsrId == System.Convert.ToInt32(e.CommandArgument) select c).FirstOrDefault(); mstrDet_bus.Delete(mstrDetDel); mstr.Delete(mstrDel); usr.Delete(usrDel); BindDataGridView(); } else if (e.CommandName == "CancelUpdate") { gvAdmin.EditIndex = -1; BindDataGridView(); } else if (e.CommandName == "UpdateRow") { int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex; FileUpload file = ((FileUpload)gvAdmin.Rows[rowIndex].FindControl("fuploadEdit")); string UsrName = ((TextBox)gvAdmin.Rows[rowIndex].FindControl("EditUsrName")).Text; string Login = ((TextBox)gvAdmin.Rows[rowIndex].FindControl("EditLogin")).Text; string Phone = ((TextBox)gvAdmin.Rows[rowIndex].FindControl("EditPhone")).Text; string Password = ((TextBox)gvAdmin.Rows[rowIndex].FindControl("EditPassword")).Text; int RoleId = Int32.Parse(((Label)gvAdmin.Rows[rowIndex].FindControl("RoleID_Present")).Text); string Address = ((TextBox)gvAdmin.Rows[rowIndex].FindControl("EditAddress")).Text; string Email = ((TextBox)gvAdmin.Rows[rowIndex].FindControl("EditEmail")).Text; string LoginOld = ((HiddenField)gvAdmin.Rows[rowIndex].FindControl("LoginOld")).Value; string PasswordOld = ((HiddenField)gvAdmin.Rows[rowIndex].FindControl("PasswordOld")).Value; bool Active = ((CheckBox)gvAdmin.Rows[rowIndex].FindControl("EditActive")).Checked; string Avatar = ""; HiddenField avaold = (HiddenField)gvAdmin.Rows[rowIndex].FindControl("EditAvatar"); string AvatarOld = ""; if (avaold == null) { AvatarOld = ""; } else { AvatarOld = avaold.Value; } if (file.HasFile) { string fname = file.FileName; string fpath = Server.MapPath("/Admin/Images/avatar/"); fpath = fpath + @"/" + file.FileName; string getext = Path.GetExtension(file.PostedFile.FileName); string filename = Path.GetFileNameWithoutExtension(file.PostedFile.FileName); string strFilePath = filename + getext; Avatar = strFilePath; if (getext != ".JPEG" && getext != ".jpeg" && getext != ".JPG" && getext != ".jpg" && getext != ".png" && getext != ".tif" && getext != ".tiff") { ScriptManager.RegisterClientScriptBlock(this, GetType(), "alertMessage", "alert('Chọn ảnh!!')", true); return; } else { file.SaveAs(Server.MapPath(@"~/Admin/Images/avatar/" + strFilePath)); ViewState["fname"] = fname; ViewState["fPath"] = @"~/Admin/Images/avatar/" + strFilePath; } } else if (Avatar == AvatarOld) { Avatar = AvatarOld; } else if (Avatar == "") { Avatar = "default.jpg"; } // kiểm tra password nếu thay đổi thì mới encrypt if (Password != PasswordOld) { Password = EncryptHelper.Encrypt(Password); } // kiểm tra tồn tại login trùng thì ko được update if (Login != LoginOld) { if (IsExists(Login)) { ScriptManager.RegisterClientScriptBlock(this, GetType(), "alertMessage", "alert('Bị trùng')", true); return; } } // update vô Usr trước Usr result = (from c in usr.GetAll() where c.UsrId == System.Convert.ToInt32(e.CommandArgument) select c).FirstOrDefault(); if (result != null) { result.Password = Password; result.UsrName = UsrName; result.Address = Address; result.Login = Login; result.Email = Email; result.Phone = Phone; result.DateEdit = DateTime.Now; result.Active = Active; result.Avatar = Avatar; usr.Update(result); } // Update vô MstrDet MstrDet mstrdet = (from c in mstrDet_bus.GetAll() where c.MstrId == System.Convert.ToInt32(e.CommandArgument) && c.RoleId == RoleId select c).FirstOrDefault(); if (mstrdet != null) { mstrdet.AddBy = Master.UsrId1; mstrDet_bus.Update(mstrdet); } gvAdmin.EditIndex = -1; BindDataGridView(); Response.Redirect(Request.RawUrl); ScriptManager.RegisterClientScriptBlock(this, GetType(), "alertMessage", "alert('Thành công')", true); } else if (e.CommandName == "InsertRow") { int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex; FileUpload fileInsert = ((FileUpload)gvAdmin.FooterRow.FindControl("fuploadInsert")); string UsrName = ((TextBox)gvAdmin.FooterRow.FindControl("InsertUsrName")).Text; string Login = ((TextBox)gvAdmin.FooterRow.FindControl("InsertLogin")).Text; string Phone = ((TextBox)gvAdmin.FooterRow.FindControl("InsertPhone")).Text; string Password = EncryptHelper.Encrypt(((TextBox)gvAdmin.FooterRow.FindControl("InsertPassword")).Text); string AddBy = ((TextBox)gvAdmin.FooterRow.FindControl("InsertAddBy")).Text; int RoleId = Int32.Parse(((DropDownList)gvAdmin.FooterRow.FindControl("InsertRoleName")).SelectedValue); string Address = ((TextBox)gvAdmin.FooterRow.FindControl("InsertAddress")).Text; string Email = ((TextBox)gvAdmin.FooterRow.FindControl("InsertEmail")).Text; bool Active = ((CheckBox)gvAdmin.FooterRow.FindControl("InsertActive")).Checked; if (UsrName == "" || Address == "" || Phone == "" || Email == "" || Password == "" || Login == "" || AddBy == "") { ScriptManager.RegisterClientScriptBlock(this, GetType(), "alertMessage", "alert('Chưa nhập đủ thông tin')", true); return; } string Avatar = ""; if (fileInsert.HasFile) { string fname = fileInsert.FileName; string fpath = Server.MapPath("/Admin/Images/avatar/"); fpath = fpath + @"/" + fileInsert.FileName; string getext = Path.GetExtension(fileInsert.PostedFile.FileName); string filename = Path.GetFileNameWithoutExtension(fileInsert.PostedFile.FileName); Avatar = filename; string strFilePath = filename + getext; if (getext != ".JPEG" && getext != ".jpeg" && getext != ".JPG" && getext != ".jpg" && getext != ".png" && getext != ".tif" && getext != ".tiff") { Page.ClientScript.RegisterStartupScript(typeof(Page), "successfull", "alert('Please upload only jpeg, jpg,png,tif,tiff'); window.location = 'ParivarRegistration.aspx';", true); } else { fileInsert.SaveAs(Server.MapPath(@"~/Admin/Images/avatar/" + strFilePath)); ViewState["fname"] = fname; ViewState["fPath"] = @"~/Admin/Images/avatar/" + strFilePath; } } if (Avatar == "") { Avatar = "default"; } if (!MyLibrary.IsValidEmailAddress(Email)) { ScriptManager.RegisterClientScriptBlock(this, GetType(), "alertMessage", "alert('Email chưa đúng')", true); return; } Usr result = new Usr { Password = Password, UsrName = UsrName, Address = Address, Login = Login, Email = Email, Phone = Phone, DateEdit = null, Active = Active, Avatar = Avatar, DateAdd = DateTime.Now }; usr.Insert(result); Mstr rs1 = new Mstr { MstrId = usr.GetLastestId() }; mstr.Insert(rs1); MstrDet mstrDet = new MstrDet { MstrId = usr.GetLastestId(), RoleId = RoleId, AddDate = DateTime.Now, AddBy = Master.UsrId1 }; mstrDet_bus.Insert(mstrDet); BindDataGridView(); } }