Asp.net Tree角色 相应权限的显示 与选中子节点 该子节点以后的全选中,修改权限

//显示 所有权限 并勾上该角色的权限

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            this.DropDownList1.DataSource = SQLHelper.SelectData("select * from [Role]");
            this.DropDownList1.DataTextField = "R_Name";
            this.DropDownList1.DataValueField = "R_ID";
            this.DropDownList1.DataBind();

            ydt = SQLHelper.SelectData("select M_ID from RoleMenuCenter where R_ID="+int.Parse(this.DropDownList1.SelectedValue.ToString()));

            dt = SQLHelper.SelectData("select * from Menu");
            inittreeview();
        }


         }
    DataTable dt = null;
    DataTable ydt = null;
    public void inittreeview()
    {
        this.TreeView1.Nodes.Clear();

        DataRow[] drs = dt.Select("M_Parent=0");
        foreach (DataRow dr in drs)
        {
            TreeNode tn = new TreeNode();
            tn.Text = dr["M_Name"].ToString();
            tn.Value = dr["M_ID"].ToString();
            foreach (DataRow ydr in ydt.Rows)
            {
                if (ydr["M_ID"].ToString().Trim() == dr["M_ID"].ToString().Trim())
                {
                    tn.Checked = true;
                    break;
                }
            }
            inittreeleaf(tn);
            this.TreeView1.Nodes.Add(tn);
        }
    }
    public void inittreeleaf(TreeNode tn)
    {
        DataRow[] drs = dt.Select("M_Parent="+tn.Value);
        foreach (DataRow dr in drs)
        {
            TreeNode ctn = new TreeNode();
            ctn.Text = dr["M_Name"].ToString();
            ctn.Value = dr["M_ID"].ToString();
            foreach (DataRow ydr in ydt.Rows)
            {
                if (ydr["M_ID"].ToString().Trim() == dr["M_ID"].ToString().Trim())
                {
                    ctn.Checked = true;
                    break;
                }
            }
            tn.ChildNodes.Add(ctn);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int roleid = int.Parse(this.DropDownList1.SelectedValue.ToString());
        SQLHelper.UpdateData("delete from RoleMenuCenter where R_ID="+roleid);

        foreach (TreeNode tn in this.TreeView1.Nodes)
        {
            if (tn.Checked)
            {
                int mid = int.Parse(tn.Value);
                SQLHelper.UpdateData("insert into RoleMenuCenter values("+roleid+","+mid+",0,0,0,0)");
                foreach (TreeNode ctn in tn.ChildNodes)
                {
                    if (ctn.Checked)
                    {
                        int cmid = int.Parse(ctn.Value);
                        SQLHelper.UpdateData("insert into RoleMenuCenter values(" + roleid + "," + cmid + ",0,0,0,0)");
                    }
                }
            }
        }
    }

     //角色改变 相应的权限也改变
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ydt = SQLHelper.SelectData("select M_ID from RoleMenuCenter where R_ID=" + int.Parse(this.DropDownList1.SelectedValue.ToString()));
        dt = SQLHelper.SelectData("select * from Menu");
        inittreeview();
        this.TreeView1.ExpandAll();
    }
}

// JScript 文件
function public_GetParentByTagName(element, tagName)
{
    var parent = element.parentNode;
    var upperTagName = tagName.toUpperCase();
    //如果这个元素还不是想要的tag就继续上溯
    while (parent && (parent.tagName.toUpperCase() != upperTagName))
    {
        parent = parent.parentNode ? parent.parentNode : parent.parentElement;
    }
    return parent;
}

//设置节点的父节点Cheched——该节点可访问,则他的父节点也必能访问
function setParentChecked(objNode)
{
    var objParentDiv = public_GetParentByTagName(objNode,"div");
    if(objParentDiv==null || objParentDiv == "undefined")
    {
        return;
    }
    var objID = objParentDiv.getAttribute("ID");
    objID = objID.substring(0,objID.indexOf("Nodes"));
    objID = objID+"CheckBox";
    var objParentCheckBox = document.getElementById(objID);
    if(objParentCheckBox==null || objParentCheckBox == "undefined")
    {
        return;
    }
    if(objParentCheckBox.tagName!="INPUT" && objParentCheckBox.type == "checkbox")
    return;
    objParentCheckBox.checked = true;
    setParentChecked(objParentCheckBox);
}

//设置节点的子节点uncheched——该节点不可访问,则他的子节点也不能访问
function setChildUnChecked(divID)
{
    var objchild = divID.children;
    var count = objchild.length;
    for(var i=0;i<objchild.length;i++)
    {
        var tempObj = objchild[i];
        if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox")
        {
            tempObj.checked = false;
        }
        setChildUnChecked(tempObj);
    }
}

//设置节点的子节点cheched——该节点可以访问,则他的子节点也都能访问
function setChildChecked(divID)
{
    var objchild = divID.children;
    var count = objchild.length;
    for(var i=0;i<objchild.length;i++)
    {
        var tempObj = objchild[i];
        if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox")
        {
            tempObj.checked = true;
        }
        setChildChecked(tempObj);
    }
}

//触发事件
function CheckEvent()
{

    var objNode = event.srcElement;

    if(objNode.tagName!="INPUT" || objNode.type!="checkbox")
    return;

    if(objNode.checked==true)
    {
        setParentChecked(objNode);
        var objID = objNode.getAttribute("ID");
        var objID = objID.substring(0,objID.indexOf("CheckBox"));
        var objParentDiv = document.getElementById(objID+"Nodes");
        if(objParentDiv==null || objParentDiv == "undefined")
        {
            return;
        }
        setChildChecked(objParentDiv);
    }
    else
    {
        var objID = objNode.getAttribute("ID");
        var objID = objID.substring(0,objID.indexOf("CheckBox"));
        var objParentDiv = document.getElementById(objID+"Nodes");
        if(objParentDiv==null || objParentDiv == "undefined")
        {
            return;
        }
        setChildUnChecked(objParentDiv);
    }
}

 

 //修改权限

 

protected void Button1_Click(object sender, EventArgs e)
    {
        int roleid = int.Parse(this.DropDownList1.SelectedValue.ToString());
        SQLHelper.UpdateData("delete from RoleMenuCenter where R_ID="+roleid);

        foreach (TreeNode tn in this.TreeView1.Nodes)
        {
            if (tn.Checked)
            {
                int mid = int.Parse(tn.Value);
                SQLHelper.UpdateData("insert into RoleMenuCenter values("+roleid+","+mid+",0,0,0,0)");
                foreach (TreeNode ctn in tn.ChildNodes)
                {
                    if (ctn.Checked)
                    {
                        int cmid = int.Parse(ctn.Value);
                        SQLHelper.UpdateData("insert into RoleMenuCenter values(" + roleid + "," + cmid + ",0,0,0,0)");
                    }
                }
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值