Exemplo n.º 1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            string state    = FormState; // 获取当前的状态
            string deptName = "";

            if (state == "添加")
            {
                // 遍历Tree根节点下的所有子节点,找到添加的新节点
                foreach (TreeNode node in tvwDepart.Nodes[0].Nodes)
                {
                    if (node.Tag.ToString() == "new")
                    {
                        deptName = node.Text.Trim(); // 获取添加节点的名字
                        break;
                    }
                }

                if (deptName == "" || deptName == "请输入新部门名称")
                {
                    UnitCommon.InfoMsg("请输入新部门名称!");
                    return;
                }

                Department dept = new Department();
                dept.Name = deptName;
                DepartmentManager deptMgr = new DepartmentManager();
                bool succ = deptMgr.AddDept(dept);
                if (succ)             // 保存成功
                {
                    FormState = "查看"; // 注意不要写成formState
                    BuildDeptTree();
                }
                else
                {
                    UnitCommon.ErrorMsg("保存新部门失败!");
                }

                return;
            }
            else if (state == "修改")
            {
                if (deptofModified != null)
                {
                    int    id   = int.Parse(deptofModified.Tag.ToString());
                    string name = deptofModified.Text.Trim();
                    if (name == "")
                    {
                        UnitCommon.WarnMsg("部门名称不能为空!");
                        return;
                    }

                    Department dept = new Department();
                    dept.Id   = id;
                    dept.Name = name;
                    DepartmentManager deptMgr = new DepartmentManager();
                    bool succ2 = deptMgr.UpadateDept(dept);
                    if (succ2)
                    {
                        FormState = "查看";
                        BuildDeptTree();
                    }
                    else
                    {
                        UnitCommon.ErrorMsg("修改部门信息失败!");
                    }
                }
            }
        }