XML权限菜单文件
<?xml version="1.0" encoding="utf-8" ?> <Menus> <MenuFunction> <menuCode>10</menuCode> <menuName>系统管理</menuName> <parentMenuCode>-1</parentMenuCode> <url /> <level>1</level> <menuType>1</menuType> <isVisible>1</isVisible> <toRoot>10,-1</toRoot> <title>成员管理</title> <imgSrc>User.gif</imgSrc> <isLeaf>0</isLeaf> </MenuFunction> <MenuFunction> <menuCode>20</menuCode> <menuName>项目管理</menuName> <parentMenuCode>-1</parentMenuCode> <url /> <level>1</level> <menuType>1</menuType> <isVisible>1</isVisible> <toRoot>20,-1</toRoot> <title>项目管理</title> <imgSrc></imgSrc> <isLeaf>0</isLeaf> </MenuFunction> <MenuFunction> <menuCode>1010</menuCode> <menuName>用户管理</menuName> <parentMenuCode>10</parentMenuCode> <url>UserList</url> <level>2</level> <menuType>1</menuType> <isVisible>1</isVisible> <toRoot>1010,10,-1</toRoot> <title>用户管理</title> <imgSrc>Member.gif</imgSrc> <isLeaf>0</isLeaf> </MenuFunction> <MenuFunction> <menuCode>101010</menuCode> <menuName>添加</menuName> <parentMenuCode>1010</parentMenuCode> <url>Add</url> <level>3</level> <menuType>3</menuType> <isVisible>1</isVisible> <toRoot>101010,1010,10,-1</toRoot> <title>添加</title> <imgSrc /> <isLeaf>1</isLeaf> </MenuFunction> <MenuFunction> <menuCode>101020</menuCode> <menuName>分配角色</menuName> <parentMenuCode>1010</parentMenuCode> <url></url> <level>3</level> <menuType>3</menuType> <isVisible>0</isVisible> <toRoot>101020,1010,10,-1</toRoot> <title>分配角色</title> <imgSrc /> <isLeaf>1</isLeaf> </MenuFunction> <MenuFunction> <menuCode>1020</menuCode> <menuName>角色管理</menuName> <parentMenuCode>10</parentMenuCode> <url>RoleList</url> <level>2</level> <menuType>1</menuType> <isVisible>1</isVisible> <toRoot>1020,10,-1</toRoot> <title>角色管理</title> <imgSrc>Role.gif</imgSrc> <isLeaf>0</isLeaf> </MenuFunction> <MenuFunction> <menuCode>102010</menuCode> <menuName>设置权限</menuName> <parentMenuCode>1020</parentMenuCode> <url></url> <level>3</level> <menuType>3</menuType> <isVisible>0</isVisible> <toRoot>102010,1020,10,-1</toRoot> <title>设置权限</title> <imgSrc>key.png</imgSrc> <isLeaf>1</isLeaf> </MenuFunction> <MenuFunction> <menuCode>102020</menuCode> <menuName>添加角色</menuName> <parentMenuCode>1020</parentMenuCode> <url>AddRole</url> <level>3</level> <menuType>3</menuType> <isVisible>1</isVisible> <toRoot>102020,1020,10,-1</toRoot> <title>添加角色</title> <imgSrc></imgSrc> <isLeaf>1</isLeaf> </MenuFunction> </Menus>
//菜单视图 DataView DV; //角色视图 DataView DVUser; //角色名称 string RoleName = string.Empty; //角色编码 public string RoleCode = string.Empty; //用户UserID string UserID = string.Empty; Business.User.RoleAction Roleaction = new Business.User.RoleAction(); Business.User.UserInfo UserInfo = new Business.User.UserInfo(); DataSet DS = new DataSet(); ArrayList RoleCodeArray = new ArrayList(); private void RoleSetRole_Load(object sender, EventArgs e) { //根据角色编码获取UserID string UserSql = string.Format(@"select UserID from UserOfRole where 1=1 {0}", RoleCode != "" ? string.Format(" and RoleCode='{0}'", RoleCode) : "" ); //如果该角色分配给了用户就取出该用户的UserID if (UserInfo.GetDataTableOption(UserSql, DBType.Sql).Rows.Count > 0) { UserID = UserInfo.GetDataTableOption(UserSql, DBType.Sql).Rows[0][0] + ""; } //根据角色编码获取功能节点 string RoleActionSql = string.Format("select MenuCode from RoleAction where 1=1 {0}", RoleCode != "" ? string.Format(" and RoleCode='{0}'", RoleCode) : "" ); //该角色对应的功能点视图 DVUser = Roleaction.GetDataTableOption(RoleActionSql, DBType.Sql).DefaultView; //加载树 BindTree(); treeView1.ExpandAll(); } //创建权限树 public void BindTree() { DS.ReadXml("Menu.xml"); DV = DS.Tables[0].DefaultView; foreach (DataRowView Drv in DV) { if (string.Equals(Drv["parentMenuCode"], "-1")) { TreeNode TN = new TreeNode(); TN.Text = Drv["menuName"] + ""; TN.Tag = Drv["menuCode"] + ""; //如果该角色编码中存在该功能点就选中 foreach (DataRowView RV in DVUser) { if (object.Equals(Drv["menuCode"], RV["menuCode"])) TN.Checked = true; } treeView1.Nodes.Add(TN); CreateChildNodes(TN); } } } //创建子节点 public void CreateChildNodes(TreeNode ParentNode) { foreach (DataRowView Drv in DV) { if (object.Equals(Drv["parentMenuCode"], ParentNode.Tag)) { TreeNode Node = new TreeNode(); Node.Text = Drv["menuName"] + ""; Node.Tag = Drv["menuCode"] + ""; //如果该角色编码中存在该功能点就选中 foreach (DataRowView RV in DVUser) { if (object.Equals(Drv["menuCode"], RV["menuCode"])) Node.Checked = true; } //父节点添加对应的子节点 ParentNode.Nodes.Add(Node); CreateChildNodes(Node); } } } private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) { if (e.Action != TreeViewAction.Unknown) { //选中(取消选中)父节点,选中(取消选中)该节点下面的子结点 CheckAllChildNodes(e.Node, e.Node.Checked); //选中(取消选中)子节点,选中(取消选中)该节点的父结点 CheckAllParentNodes(e.Node, e.Node.Checked); } } //选中(取消选中)父节点,选中(取消选中)该节点下面的子结点 public void CheckAllChildNodes(TreeNode ParentNode, bool NodeChk) { foreach (TreeNode TN in ParentNode.Nodes) { TN.Checked = NodeChk; CheckAllChildNodes(TN, NodeChk); } } //选中(取消选中)子节点,选中(取消选中)该节点的父结点 public void CheckAllParentNodes(TreeNode ChildNode, bool NodeChk) { if (ChildNode.Parent != null) { ChildNode.Parent.Checked = HasParentNode(ChildNode.Parent); CheckAllParentNodes(ChildNode.Parent, NodeChk); } } //根据当前子节点的状态,选中或不选中父节点 public bool HasParentNode(TreeNode TN) { foreach (TreeNode ChildNode in TN.Nodes) { if (ChildNode.Checked) { return true; } HasParentNode(ChildNode); } return false; } //遍历TreeView中所有选中节点 //然后将选中的RoleCode插入到ArrayList中 public void GetNode(TreeNodeCollection tc) { foreach (TreeNode TNode in tc) { if (TNode.Checked) { RoleCodeArray.Add(TNode.Tag); } GetNode(TNode.Nodes); } } private void tsbSave_Click(object sender, EventArgs e) { TreeNodeCollection tc = treeView1.Nodes; GetNode(tc); ArrayList SqlArray = new ArrayList(); //删除该角色之前所有功能节点 string Delsql = string.Format(@"delete from RoleAction where 1=1 {0}", RoleCode != "" ? string.Format(" and RoleCode='{0}'", RoleCode) : "" ); //将功能节点数组,角色编码,用户UserID插入到权限表中 string AddSql = Roleaction.CreateRolePriviewSql(RoleCodeArray, RoleCode, UserID); //将删除sql,插入sql添加到ArrayList数组中 SqlArray.Add(Delsql); SqlArray.Add(AddSql); //执行事务 if (Roleaction.ExecTrans(SqlArray, DBType.Sql)) MessageBox.Show("修改成功"); else MessageBox.Show("修改失败"); }