treeview

前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="power.aspx.cs" Inherits="admin_module_power" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>User Role Power</title>
    <script type="text/javascript" language="javascript" src="../js/Control.js"></script>
    <script type="text/javascript" language="javascript">
        window.document.attachEvent("onkeydown",Control.HidenKeyF5);
    </script>
    <script language="JavaScript">
//获取元素指定tagName的父元素
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);
    }
}

</script>
    <link href="../css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
     <table border="0" cellpadding="0" cellspacing="0" class="record"
            width="100%">
            <tr>
                <th nowrap="nowrap">
                    CMS User Role Power</th>
            </tr>
            <tr>
                <td colspan="2">
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    &nbsp;
   
        <asp:TreeView ID="tv_Roleview" runat="server">
        </asp:TreeView>   
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    &nbsp;
   
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
               
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" CssClass="button" />
                    <asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="button" OnClick="btnCancel_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="2" style="height: 19px">
                    &nbsp;</td>
            </tr>
        </table>
</form>
</body>
</html>

后台代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 System.Data.SqlClient;
using CMS.BLL;

public partial class admin_module_power : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
    string selectnodesAll = "";
    string[] powers;
   ArrayList arr = new ArrayList();
    protected void Page_Load(object sender, EventArgs e)
    {
        //
        if (Session["userId"] == null || Session["userId"].ToString() == "")
        {
            Page.RegisterStartupScript("re_alert", "<script>parent.window.navigate('../login.aspx');</script>");

        }

        this.tv_Roleview.Attributes.Add("onclick", "return CheckEvent();");
       
        if (!Page.IsPostBack)
        {

            arr.AddRange(BindPower());

            CreateTree(tv_Roleview.Nodes, "0");

           
        }
      
 
    }

    public DataSet GetData()
    {
       
       // conn.Open();//
        string sql = "select * from cms_section  where section_status=2 order by section_pos";

        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "sections");
        return ds;
        //ds.Dispose();
        da.Dispose();
       
      //  conn.Close();
    }

   private string[] BindPower()
    {
        conn.Open();
        string role_id = Request["role_id"].ToString();
        int roleId = int.Parse(role_id);
        string sql = "select * from cms_role where role_id = " + roleId;
        SqlCommand sc = new SqlCommand(sql, conn);
        SqlDataReader sdr = sc.ExecuteReader(); 
        string power_list="";
        while (sdr.Read())
        {
            power_list = sdr["role_power"].ToString();            //...
        }

      string[] power = power_list.Split(',');//","   
       
        sc.Dispose();
        sdr.Dispose();
        conn.Close();

    return power;
    }


    private void CreateTree(TreeNodeCollection Nds, string ParentId)
    {
        DataRow[] rows = GetData().Tables["sections"].Select("section_parent='" + ParentId + "'");       

        foreach (DataRow dr in rows)
        {
            TreeNode treenode = new TreeNode();
            treenode.SelectAction = TreeNodeSelectAction.None;
            treenode.Value = dr["section_id"].ToString().Trim();
            treenode.Text = dr["section_name"].ToString().Trim();
            treenode.Checked = false;
            treenode.Expanded = true;
            treenode.SelectAction = TreeNodeSelectAction.None;
            //int n = Array.BinarySearch(powers, dr["section_id"].ToString());
            if (arr.Contains(dr["section_id"].ToString().Trim()))
            {
                treenode.Checked = true;
            }
           
          

            treenode.ShowCheckBox = true;
           
            Nds.Add(treenode);   

            int id = int.Parse(dr["section_id"].ToString().Trim());
            CreateTree(treenode.ChildNodes, treenode.Value);
        }
    }



    //Submit the role power
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        conn.Open();
        string role_id = Request["role_id"].ToString();
        int roleId = int.Parse(role_id);
  

        for (int i = 0; i < this.tv_Roleview.Nodes.Count; i++)
        {
            TreeNode node = this.tv_Roleview.Nodes[i];
            if (node.Checked == true)
            {
                selectnodesAll += node.Value + ",";
            }
            if (node.ChildNodes.Count > 0)
            {
                GetNodes(node);
            }

        }
        //Response.Write(selectnodesAll);    //strID

       /* if (Session["rolePower"].ToString() == "")
        {
            string insertSQL = "update into cms_role(role_power) values(@role_power) where role_id = " + roleId;
            SqlCommand cmd = new SqlCommand(insertSQL, conn);
            cmd.Parameters.Add(new SqlParameter("@role_power", SqlDbType.NText));
            cmd.Parameters["@role_power"].Value = selectnodesAll;
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Response.Write("Insert failed " + ex.Message.ToString());
            }
        }
        else
        {*/
            string updateSQL = "update cms_role set role_power=@role_power where role_id = " + roleId;
            SqlCommand cmd = new SqlCommand(updateSQL, conn);
            cmd.Parameters.Add(new SqlParameter("@role_power", SqlDbType.NText));
            cmd.Parameters["@role_power"].Value = selectnodesAll;
            try
            {
                cmd.ExecuteNonQuery();
                string log_date = DateTime.Now.ToString();
                int log_user = int.Parse(Session["userId"].ToString());
                cmsLog cmslog = new cmsLog();
                cmslog.InsertLog(log_date, log_user, 1, "User " + Session["userLogin"].ToString() + " Update Role Power Succeed", roleId);
                Page.RegisterStartupScript("message", "<script>alert('Update Succeed!');</script>");
            }
            catch (Exception ex)
            {
                Response.Write("Update failed " + ex.Message.ToString());
            }
     //   }
        conn.Close();

       
    }

    // to get tree nodes
    private void GetNodes(TreeNode selectTn)
    {
        if (selectTn == null) return;
        foreach (TreeNode tn in selectTn.ChildNodes)
        {
            if (tn.Checked == true)
            {
                selectnodesAll += tn.Value + ",";
            }
            GetNodes(tn);
        }
    }


    //return to role_list
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("role.aspx");
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值