📄 distributerole.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Office.Model;
using Office.BLL;
public partial class SysManage_RoleManage_DistributeRole : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//从请求获得角色ID
String roleId = Request.QueryString["RID"];
if (roleId != null)
{
ViewState["RoleId"] = roleId;
RoleInfo role = RoleInfoManager.GetRoleInfoByRoleId(Int32.Parse(roleId));
lblCurrentRole.Text = role.RoleName;
}
}
DisplayRoleRightInfo(Int32.Parse(ViewState["RoleId"].ToString()));
}
//根据角色ID显示角色的相关权限
protected void DisplayRoleRightInfo(Int32 roleId)
{
//得到角色权限信息
IList<RoleRight> currentRoleParentNodes = RoleRightManager.GetRoleRightsByRoleId(roleId);
//创建一个集合将用户所有的角色权限添加进去
ArrayList arrRoleParentNodes = new ArrayList();
foreach (RoleRight roleRight in currentRoleParentNodes)
{
arrRoleParentNodes.Add(roleRight.Node.NodeId.ToString());
}
//获得数据中所有菜单信息
IList<SysFun> sysFuns = SysFunManager.GetAllParentNodeInfo();
foreach (SysFun sysFun in sysFuns)
{
string nodeId = sysFun.NodeId.ToString();//ID
string displayName = sysFun.DisplayName;//Name
//加载用户控件
UserControl roleContol = (UserControl)LoadControl(@"RoleUserControl.ascx");
//从用户控件中查找名为"hidParentMenu",并将value设为菜单ID
HtmlInputHidden hidParentMenu = (HtmlInputHidden)roleContol.FindControl("hidParentMenu");
hidParentMenu.Value = nodeId;
//从用户控件中查找名为"hidRoleId",并将Value属性设为角色ID
HtmlInputHidden hidRoleId = (HtmlInputHidden)roleContol.FindControl("hidRoleId");
hidRoleId.Value = roleId.ToString();
//从用户控件中查找"chkParentMenu",将Text属性设为菜单名称
CheckBox chkParentMenu = (CheckBox)roleContol.FindControl("chkParentMenu");
chkParentMenu.Text = displayName;
//判断用户角色权限集合里面是否有菜单的ID,true为选中,false为没选中
if (arrRoleParentNodes.Contains(nodeId))
chkParentMenu.Checked = true;
//最后将其添加到控件容器中
phRoleDistribute.Controls.Add(roleContol);
}
}
protected void btnSave_ServerClick(object sender, EventArgs e)
{
int roleId = int.Parse(ViewState["RoleId"].ToString());
//根据用户角色获得所有节点信息
IList<RoleRight> currentRoleRights = RoleRightManager.GetRoleRightsByRoleId(roleId);
//将得到的用户角色信息存放到集合中
ArrayList arrRoleNodes = new ArrayList();
foreach (RoleRight roleRight in currentRoleRights)
{
arrRoleNodes.Add(roleRight.Node.NodeId.ToString());
}
//Response.Write(phRoleDistribute.Controls.Count.ToString());
//遍历每一个用户控件
foreach (Control ct in phRoleDistribute.Controls)
{
//实例化父菜单
CheckBox chk = (CheckBox)ct.FindControl("chkParentMenu");
HtmlInputHidden hih = (HtmlInputHidden)ct.FindControl("hidParentMenu");
//如果数据库不存在我们选中的节点,则添加该节点记录
if (chk.Checked)
{
//如果复选框为选中状态而数据库中又没有,则添加
if (!arrRoleNodes.Contains(hih.Value))
{
RoleRightManager.InsertRoleRight(roleId.ToString(), hih.Value);
}
}
else
{
//如果复选框没有选中而数据中又存在,则删除
if (arrRoleNodes.Contains(hih.Value))
{
RoleRightManager.DeleteRoleRight(roleId.ToString(), hih.Value);
}
}
//实例化子菜单,添加删除子菜单算法同上
CheckBoxList chklist = (CheckBoxList)ct.FindControl("chkChildMenu");
foreach (ListItem li in chklist.Items)
{
if (li.Selected)
{
if (!arrRoleNodes.Contains(li.Value))
{
RoleRightManager.InsertRoleRight(roleId.ToString(), li.Value);
}
}
else
{
if (arrRoleNodes.Contains(li.Value))
{
RoleRightManager.DeleteRoleRight(roleId.ToString(), li.Value);
}
}
}
}
Response.Write("<script>alert('权限已生效!');self.document.location.href='RoleManage.aspx'</script>");
}
protected void btnRet_ServerClick(object sender, EventArgs e)
{
Response.Redirect("RoleManage.aspx");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -