#region 绑定角色
    /// <summary>
    /// 绑定权限信息
    /// </summary>
    protected void BindgvRoles()
    {
        string sql = "select * from GM_FileKind";
        DataTable dt = OADBHelper.GetDataTable(sql);
        this.gvRoles.DataSource = dt;
        this.DataBind();
    }
    #endregion


    #region TreeView 表员工表的显示至TreeView控件
    /// <summary>
    /// Tree绑定
    /// </summary>
    public void BindTree()
    {
        this.tvUsers.Nodes.Clear();
        string sql = "select * from Accounts_UserDepartment";
        DataView dv = OADBHelper.GetDataTable(sql).DefaultView;
        //dv.RowFilter = "Kind=0";
        foreach (DataRowView drv in dv)
        {
            TreeNode tn = new TreeNode();
            tn.Text = drv["Dept"].ToString();
            tn.Value = drv["AutoID"].ToString();
            //tn.NavigateUrl = drv["KURL"].ToString();
            tn.SelectAction = TreeNodeSelectAction.Expand;
            tn.Expanded = false;
            this.tvUsers.Nodes.Add(tn);
            addChildNodes(tn);
        }
    }


    /// <summary>
    /// 绑定子节点
    /// </summary>
    /// <param name="tn"></param>
    public void addChildNodes(TreeNode tn)
    {
        string sql = "select UserID,RealName from Accounts_Users where Dept='" + tn.Text + "'";
        DataView dv = OADBHelper.GetDataTable(sql).DefaultView;
        foreach (DataRowView drv in dv)
        {
            TreeNode childtn = new TreeNode();
            childtn.Text = drv["RealName"].ToString();
            childtn.Value = drv["UserID"].ToString();
            //childtn.NavigateUrl = drv["KURL"].ToString();
            childtn.Expanded = false;
            tn.ChildNodes.Add(childtn);
            addChildNodes(childtn);
        }
    }

    #endregion


    #region 从数据库中读取数据,然后勾选根节点的相应的CheckBox
    /// <summary>
    /// 从数据库中读取数据,然后勾选根节点的相应的CheckBox
    /// </summary>
    /// <param name="RoleId"></param>
    protected void BindTreeRoot(int RoleId, TreeNodeCollection treenode)
    {
        string sql = "";
        foreach (TreeNode nodes in treenode)
        {
            if (nodes.ChildNodes.Count > 0)
            {
                //查找所有权限的人的部门Id
                sql = "select Distinct[DepId] from GM_Roles where FKId=" + RoleId;

                DataView dv = OADBHelper.GetDataTable(sql).DefaultView;

                foreach (DataRowView drv in dv)
                {

                    foreach (TreeNode node in treenode)
                    {

                        if (drv["DepId"].ToString() == node.Value.ToString())
                        {
                            node.Checked = true;
                        }

                        BindTreeRoot(RoleId, node.ChildNodes);
                    }
                }
            }
            else
            {
                //根据角色ID得到相应的的员工ID
                sql = "select UserID from GM_Roles where FKId='" + RoleId + "'";

                DataView dv = OADBHelper.GetDataTable(sql).DefaultView;

                foreach (DataRowView drv in dv)
                {

                    foreach (TreeNode node in treenode)
                    {

                        if (drv["UserID"].ToString() == node.Value.ToString())
                        {
                            node.Checked = true;
                        }

                        BindTreeRoot(RoleId, node.ChildNodes);
                    }
                }
            }

        }
    }
    #endregion


    #region 设置权限
    /// <summary>
    /// 设置权限
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lkbtnModify_Command(object sender, CommandEventArgs e)
    {
        int Id = Convert.ToInt32(e.CommandArgument.ToString());

        ViewState["RoleId"] = Id;

        BindTree();

        string sql = "select FKName from GM_FileKind where FKId=" + Id;

        lblRoles.Text = OADBHelper.GetString(sql);

        BindTreeRoot(Id, this.tvUsers.Nodes);
    }
    #endregion


    #region  权限设置操作
    /// <summary>
    /// 权限设置操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModify_Click(object sender, EventArgs e)
    {
        if (this.lblRoles.Text == "没有选择角色")
        {
            Response.Write("<script>alert('请选择一个角色名称!');</script>");
        }
        else
        {
            int RoleId = Convert.ToInt32(ViewState["RoleId"].ToString());

            string sql = "select UserId from UserId where FKId=" + RoleId;

            DeleteRoles(Convert.ToInt32(ViewState["RoleId"].ToString()));

            this.GetCheckBoxChecked(this.tvUsers.Nodes);

            Response.Write("<script>alert('角色设置成功!');</script>");

        }
    }
    #endregion

    #region  添加选定的人到角色表(checkbox选中的,插入数据库)

    /// <summary>
    /// 选择要添加的角色
    /// </summary>
    /// <param name="treeNode"></param>
    private void GetCheckBoxChecked(TreeNodeCollection treeNode)
    {
        foreach (TreeNode node in treeNode)
        {
            if (node.Checked)
            {

                if (node.ChildNodes.Count == 0)
                {
                    //执行保存   
                    int UserId = Convert.ToInt32(node.Value.ToString());

                    InertRoleMember(Convert.ToInt32(ViewState["RoleId"].ToString()), UserId, Convert.ToInt32(ViewState["DepatId"].ToString()));

                    this.GetCheckBoxChecked(node.ChildNodes);
                }
                else
                {

                    ViewState["DepatId"] = node.Value.ToString();

                    GetCheckBoxChecked(node.ChildNodes);

                }

            }
            else
            {
                if (node.ChildNodes.Count > 0)
                {
                    this.GetCheckBoxChecked(node.ChildNodes);
                }
            }
        }
    }
    #endregion

    #region 通过部门ID得到所有的人员Id
    /// <summary>
    /// 通过部门ID得到所有的人员Id
    /// </summary>
    /// <param name="DeptId"></param>
    /// <returns></returns>
    protected int GetUserIdByDeptId(int DeptId)
    {
        string strSql = "select Dept from Accounts_UserDepartment where AutoID=" + DeptId;

        string DeptName = OADBHelper.GetString(strSql);  //得到部门名称

        strSql = "select UserID from Accounts_Users where Dept='" + DeptName + "'";

        int UserId = Convert.ToInt32(OADBHelper.GetString(strSql));

        return UserId;
    }
    #endregion

    #region 根据选中的节点设置相应的权限人到数据库
    /// <summary>
    /// 根据选中的节点设置相应的权限人
    /// </summary>
    /// <param name="classId"></param>
    /// <param name="RoleId"></param>
    protected void InertRoleMember(int FKId, int UserId,int DeptId)
    {
        string sql = @"insert into GM_Roles(FKId,UserId,DepId)values('" + FKId + "','" + UserId + "','" + DeptId + "')";
        OADBHelper.GetInt(sql);
    }
    #endregion

    #region 根据角色Id删除相应的权限
    /// <summary>
    /// 根据角色Id删除相应的权限
    /// </summary>
    /// <param name="RoleId"></param>
    protected void DeleteRoles(int RoleId)
    {
        string sql = @"delete from GM_Roles where FKId=" + RoleId;
        OADBHelper.GetInt(sql);
    }
    #endregion

    #region C#复选框的操作
    /// <summary>
    /// 复选框改变时的操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void tvUsers_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
        SetChildChecked(e.Node);
    }

    /// <summary>

    /// 根据父节点情况来选择子节点

    /// </summary>

    /// <param name="parentNode"></param>

    private void SetChildChecked(TreeNode parentNode)
    {
        foreach (TreeNode node in parentNode.ChildNodes)
        {
            node.Checked = parentNode.Checked;
            if (node.ChildNodes.Count > 0)
            {
                SetChildChecked(node);
            }
        }
    }
    #endregion

posted on 2011-08-27 16:04  易尔购  阅读(4204)  评论(1编辑  收藏  举报