treeview(将代码复制出去就可以运行)

一 :在前台中添加js代码

<script language='javascript' type='text/javascript'>

        function client_OnTreeNodeChecked(evt) {

            var obj = getCurrentNode(evt);  //调用getCurrentNode()方法

            var treeNodeFound = false;

            var checkedState;

            if (obj.tagName == "INPUT" && obj.type == "checkbox") {

                checkedState = obj.checked;

                var curNode = getParentNode(obj);

                changeNodeState(curNode, checkedState);

            }

        }

 

        function getCurrentNode(evt) {

            var obj;

            if (window.event) obj = window.event.srcElement;

            else obj = (evt ? evt : (window.event ? window.event : null)).target;

            return obj;

        }

 

        function getParentNode(node) {

            do {

                node = node.parentNode;

            }

            while (node.tagName != "TABLE")

            return node;

        }

 

        function changeNodeState(node, state) {

            //change all the children

            if (node.nextSibling != null && node.nextSibling.tagName == "DIV") {

                var cbArr = node.nextSibling.getElementsByTagName("INPUT");

                for (var i = 0; i < cbArr.length; i++) {

                    if (cbArr[i].type == "checkbox") cbArr[i].checked = state;

                }

            }
            if (state) {

                var parentArr = node.parentNode.previousSibling.getElementsByTagName("INPUT");

                for (i = 0; i < parentArr.length; i++) {

                    if (parentArr[i].type == "checkbox") parentArr[i].checked = state;

                }

            }

        }

    </script>

 

二 :用到下列服务器控件

       <asp:Button ID="Button2" runat="server" Text="show" οnclick="Button2_Click" />
        <asp:TreeView ID="TreeView1" runat="server" >
        </asp:TreeView>
       <asp:Button ID="Button1" runat="server" Text="输出权限" οnclick="Button1_Click" />

 

三:后台代码

 

 

 

DataTable tblGrid = null;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              
                TreeView1.Attributes.Add("onclick", "client_OnTreeNodeChecked(event)");

                InittTable();
              
            }
        }
        void InittTable()
        {
            //初始化Table
            tblGrid = new DataTable();
            tblGrid.Rows.Clear();
            tblGrid.Columns.Clear();
            tblGrid.Columns.Add("ID");
            tblGrid.Columns.Add("Name");
            tblGrid.Columns.Add("pID");


            DataRow dr1 = tblGrid.NewRow();
            dr1["ID"] = "1";
            dr1["Name"] = "A";
            dr1["pID"] = "0";

            tblGrid.Rows.Add(dr1);


            DataRow dr2 = tblGrid.NewRow();
            dr2["ID"] = "2";
            dr2["Name"] = "B";
            dr2["pID"] = "0";

            tblGrid.Rows.Add(dr2);

            DataRow dr3 = tblGrid.NewRow();
            dr3["ID"] = "3";
            dr3["Name"] = "C";
            dr3["pID"] = "0";

            tblGrid.Rows.Add(dr3);

            DataRow dr4 = tblGrid.NewRow();
            dr4["ID"] = "4";
            dr4["Name"] = "A1";
            dr4["pID"] = "1";

            tblGrid.Rows.Add(dr4);

            DataRow dr5 = tblGrid.NewRow();
            dr5["ID"] = "5";
            dr5["Name"] = "A2";
            dr5["pID"] = "1";

            tblGrid.Rows.Add(dr5);

            DataRow dr6 = tblGrid.NewRow();
            dr6["ID"] = "6";
            dr6["Name"] = "B1";
            dr6["pID"] = "2";

            tblGrid.Rows.Add(dr6);

            DataRow dr7 = tblGrid.NewRow();
            dr7["ID"] = "7";
            dr7["Name"] = "B2";
            dr7["pID"] = "2";

            tblGrid.Rows.Add(dr7);

            DataRow dr8 = tblGrid.NewRow();
            dr8["ID"] = "8";
            dr8["Name"] = "C1";
            dr8["pID"] = "3";

            tblGrid.Rows.Add(dr8);

            DataRow dr9 = tblGrid.NewRow();
            dr9["ID"] = "9";
            dr9["Name"] = "C2";
            dr9["pID"] = "3";

            tblGrid.Rows.Add(dr9);

            DataRow dr10 = tblGrid.NewRow();
            dr10["ID"] = "10";
            dr10["Name"] = "C3";
            dr10["pID"] = "3";

            tblGrid.Rows.Add(dr10);

            DataRow dr11 = tblGrid.NewRow();
            dr11["ID"] = "11";
            dr11["Name"] = "D";
            dr11["pID"] = "0";

            tblGrid.Rows.Add(dr11);

            TreeNode node = new TreeNode();

            treeadd(tblGrid,0,node);  //调用treeview          
        }

         private void treeadd(DataTable dt, int Filterid, TreeNode node)
        {
            DataView subView = dt.DefaultView;    
                 subView.RowFilter = "pID = " + Filterid; //获得ID级别为当前ID的
                  foreach (DataRowView item in subView)
                    {
                      //如果上级ID为0就直接加载树下面
                        if (Filterid == 0)
                        {
                            TreeNode treenodel = new TreeNode();  //创建一个节点
                            treenodel.Text = item["Name"].ToString();// 节点名称
                            int id =Convert.ToInt32(item["ID"]);   
                            treenodel.Value = id.ToString();     //节点值
                            treenodel.ShowCheckBox = true;
                            TreeView1.Nodes.Add(treenodel);   //如果级别ID为0就直接加到根节点
                            treeadd(dt, id, treenodel);  //递归调用
                        }
                        else
                        {     
                            //不为0就加载传进来的子节点下
                  TreeNode treenodel = new TreeNode();
                                treenodel.Text = item["Name"].ToString();
                                int id = Convert.ToInt32(item["ID"]);
                                treenodel.Value = id.ToString();
                                treenodel.Expanded = true;       // 是否展开该节点
                                treenodel.ShowCheckBox = true;
                                node.ChildNodes.Add(treenodel);  //把节点加入到当前节点下,
                                treeadd(dt, id, treenodel);

                            //ShowCheckBoxes="All"
                        }
                   }
        }

       

         protected void Button1_Click(object sender, EventArgs e)
         {
             string qx = "";
             foreach (TreeNode node in TreeView1.CheckedNodes)
             {

                 if (node.Checked == true && node.Parent !=null)
                 {
                     qx += node.Value+",";

                 }
             
             }

             if (qx != string.Empty)
                 qx=qx.Substring(0, qx.Length - 1);
             Response.Write(qx);
         }

         public void ShowQx(string qx)
         {
         
             string[] qvd = null;

             if (!string.IsNullOrEmpty(qx))
             {

                 qvd = qx.Split(',');

                 for (int i = 0; i < qvd.Length; i++)
                 {
                     foreach (TreeNode node in TreeView1.Nodes)
                     {

                         foreach (TreeNode n in node.ChildNodes)
                         {
                             if (n.Value == qvd[i])
                             {
                                 n.Checked = true;
                                 node.Checked = true;
                               
                                 break;
                             }
                         }
                      
                    
                     }
                
                 }
               
             }
           
         }

         protected void Button2_Click(object sender, EventArgs e)
         {
             ShowQx("4,7,8");
         }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值