protected void Page_Load(object sender, EventArgs e) { //判断页面是否是第一次加载 if (!IsPostBack) { ddlCharge.DataSource = BLL.DepartmentManageBll.GetDeptChargeName(); ddlCharge.DataTextField = "UserName"; ddlCharge.DataValueField = "UserId"; ddlCharge.DataBind(); //判断是否为空 if (Request["DeptID"] != null) { //表示修改 btnSave.Visible = false; btnModify.Visible = true; Model.DepartmentInfo u = BLL.DepartmentManageBll.GetSingleDeptInfo(Convert.ToInt32(Request["DeptID"])); txtDeptName.Text = u.DeptName; txtDeptInfo.Text = u.DeptInfo; ddlCharge.SelectedValue = u.ManagerID;; } else { //表示添加 btnSave.Visible = true; btnModify.Visible = false; } } }
/// <summary> /// 判断部门员工===》是否显示删除按钮 /// </summary> /// <param name="u"></param> /// <returns></returns> public static DataTable JudgeDeptEmp(Model.DepartmentInfo u) { string sql = "select * from UserInfo inner join Department on UserInfo.DeptID=Department.DeptID where UserInfo.UserType=0 and Department.DeptName=@DeptName"; SqlParameter[] para = { new SqlParameter("DeptName", u.DeptName) }; return(DBHelper.ExecuteSelect(sql, para)); }
//根据用户ID删除用户信息 public static bool DelDeptInfo(Model.DepartmentInfo u) { string sql = "delete from Department where DeptID=@DeptID"; SqlParameter[] para = { new SqlParameter("DeptID", u.DeptID) }; return(DBHelper.ExecuteNonQuery(sql, para)); }
//===============================DepartmentEdit===================================== /// <summary> /// 实现部门信息的添加 /// </summary> /// <param name="u"></param> /// <returns></returns> public static bool InsertDeptInfo(Model.DepartmentInfo u) { //参数化sql语句 string strSql = "INSERT INTO [Department] ([DeptName], [ManagerID], [DeptInfo]) VALUES (@DeptName, @ManagerID, @DeptInfo)"; SqlParameter[] para = { new SqlParameter("DeptName", u.DeptName), new SqlParameter("ManagerID", u.ManagerID), new SqlParameter("DeptInfo", u.DeptInfo), }; return(DBHelper.ExecuteNonQuery(strSql, para)); }
/// <summary> /// 实现对部门管理数据的修改 /// </summary> /// <param name="u"></param> /// <returns></returns> public static bool UpdateDeptInfo(Model.DepartmentInfo u) { //参数化sql语句 string strSql = "UPDATE [Department] SET [DeptName] = @DeptName, [ManagerID] = @ManagerID, [DeptInfo] = @DeptInfo WHERE [DeptID] = @DeptID"; SqlParameter[] para = { new SqlParameter("DeptName", u.DeptName), new SqlParameter("ManagerID", u.ManagerID), new SqlParameter("DeptInfo", u.DeptInfo), new SqlParameter("DeptID", u.DeptID), }; return(DBHelper.ExecuteNonQuery(strSql, para)); }
//保存信息 protected void btnSave_Click(object sender, EventArgs e) { Model.DepartmentInfo u = new Model.DepartmentInfo(); u.DeptName = txtDeptName.Text; u.ManagerID = ddlCharge.SelectedValue; u.DeptInfo = txtDeptInfo.Text; //赋值 if (BLL.DepartmentManageBll.InsertDeptInfo(u)) { this.ClientScript.RegisterStartupScript(GetType(), "", "alert('恭喜您,添加成功!');window.parent.location='DepartmentManage.aspx'", true); } else { this.ClientScript.RegisterStartupScript(GetType(), "", "alert('很遗憾,添加失败!');", true); } }
//修改 protected void btnModify_Click(object sender, EventArgs e) { Model.DepartmentInfo u = new Model.DepartmentInfo(); u.DeptName = txtDeptName.Text; u.DeptID = Convert.ToInt32(Request["DeptID"]); u.ManagerID = ddlCharge.SelectedValue; u.DeptInfo = txtDeptInfo.Text; //赋值 if (BLL.DepartmentManageBll.UpdateDeptInfo(u)) { this.ClientScript.RegisterStartupScript(GetType(), "", "alert('恭喜您,修改成功!');window.parent.location='DepartmentManage.aspx'", true); } else { this.ClientScript.RegisterStartupScript(GetType(), "", "alert('很遗憾,修改失败!');", true); } }
/// <summary> /// 根据部门id获取单个对象 /// </summary> /// <param name="managerID">主管ID</param> /// <returns></returns> public static Model.DepartmentInfo GetSingleDeptInfo(int deptID) { string strSql = "select * from Department where DeptID=@DeptID"; SqlParameter[] para = { new SqlParameter("DeptID", deptID) }; DataTable dt = DBHelper.ExecuteSelect(strSql, para); DataRow dr = dt.Rows[0]; Model.DepartmentInfo u = new Model.DepartmentInfo(); u.DeptName = (string)dr["DeptName"]; u.DeptInfo = (string)dr["DeptInfo"]; u.DeptID = (int)dr["DeptID"]; u.ManagerID = (string)dr["ManagerID"]; return(u); }
// 判断部门员工===》是否显示删除按钮 private void IsShowDelBtnImg() { //判断部门的人数是否为0,然后为删除的依据 Model.DepartmentInfo d = new Model.DepartmentInfo(); for (int i = 0; i < gvDeptInfo.Rows.Count; i++) { d.DeptName = gvDeptInfo.Rows[i].Cells[1].Text; //循环获取部门名称 DataTable data = BLL.DepartmentManageBll.JudgeDeptEmp(d); if (data.Rows.Count > 0) //部门下有员工 ==>隐藏删除按钮 显示修改按钮 { gvDeptInfo.Rows[i].Cells[3].FindControl("imgUpdate").Visible = true; gvDeptInfo.Rows[i].Cells[3].FindControl("imgBtnDel").Visible = false; } else//部门下没有员工 ==>显示删除按钮 显示修改按钮 { gvDeptInfo.Rows[i].Cells[3].FindControl("imgUpdate").Visible = true; gvDeptInfo.Rows[i].Cells[3].FindControl("imgBtnDel").Visible = true; } } }
//命令对象 protected void gvDeptInfo_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "DeleteDeptInfo") { //表示要执行删除操作 Model.DepartmentInfo d = new Model.DepartmentInfo(); d.DeptID = Convert.ToInt32(e.CommandArgument.ToString()); if (BLL.DepartmentManageBll.DelDeptInfo(d)) { //表示删除成功 this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('恭喜您,删除成功!');", true); //即时刷新列表里面的数据 BindDeptInfo(); } else { //表示删除失败 this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('对不起,删除失败!');", true); } } }
/// <summary> /// 实现对部门管理数据的修改 /// </summary> /// <param name="u"></param> /// <returns></returns> public static bool UpdateDeptInfo(Model.DepartmentInfo u) { return(DAL.DepartmentManageDal.UpdateDeptInfo(u)); }
//===============================DepartmentEdit===================================== /// <summary> /// 实现部门信息的添加 /// </summary> /// <param name="u"></param> /// <returns></returns> public static bool InsertDeptInfo(Model.DepartmentInfo u) { return(DAL.DepartmentManageDal.InsertDeptInfo(u)); }
/// <summary> /// 判断部门员工===》是否显示删除按钮 /// </summary> /// <param name="u"></param> /// <returns></returns> public static DataTable JudgeDeptEmp(Model.DepartmentInfo u) { return(DAL.DepartmentManageDal.JudgeDeptEmp(u)); }