• TreeView绑定XML


    前台HTML:

    <asp:TreeView ID="TreeViewMU" runat="server" ShowCheckBoxes="All"  AutoGenerateDataBindings="False" ontreenodecheckchanged="TreeViewMU_TreeNodeCheckChanged">
           
    </asp:TreeView>

     

    后台CS:

    -----------绑定TreeView--------------------------------------------------------------------------

          protected void TreeVMU()
            {
                XmlDocument xmldoc = new XmlDocument();
                string spath = Server.MapPath("~\\xml\\navigation.xml");
                xmldoc.Load(spath);
                TreeViewMU.Nodes.Clear();
               
                TreeViewMU.Nodes.Add(new TreeNode(xmldoc.DocumentElement.Name));
                TreeNode tnode = new TreeNode();
                tnode = (TreeNode)TreeViewMU.Nodes[0];
                addTreeNode(xmldoc.DocumentElement,tnode);
                //TreeViewMU.ExpandAll();

            }
            private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
            {
                XmlNode xNode;//存放xml节点
                TreeNode tNode;//存放树节点
                XmlNodeList xNodeList;//存放节点集合(这里是所有子节点的集合)
                if (xmlNode.HasChildNodes)//判断是否有子节点
                {
                    xNodeList = xmlNode.ChildNodes;//子节点集合
                    for (int i = 0; i <= xNodeList.Count - 1; i++)
                    {
                        xNode = xmlNode.ChildNodes[i]; //获取当前子节点
                        TreeNode node = new TreeNode();
                        node.Text = xNode.Attributes["Name"].Value;
                        treeNode.ChildNodes.Add(node);
                        tNode = treeNode.ChildNodes[i];//获取当前树节点
                        addTreeNode(xNode, tNode);     //递归
                    }
                }
                else
                {
                    treeNode.Text = xmlNode.Attributes["Name"].Value;
                }
            }

           --------------------------选择Checkbox---------------------------------------------------
            private void CheckTreeNode(TreeNode treeNode,bool tf)
            {
                TreeNode tNode;//存放树节点
                if (treeNode.ChildNodes.Count>0)//判断是否有子节点
                {
                    for (int i = 0; i <= treeNode.ChildNodes.Count - 1; i++)
                    {
                        tNode = (TreeNode)treeNode.ChildNodes[i]; //获取当前子节点
                       
                        tNode.Checked = tf;

                        CheckTreeNode(tNode, tf);     //递归
                    }
                }
                else
                {
                  
                     treeNode.Checked = tf;
                   
                }
            }

            protected void TreeViewMU_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
            {

                if (TreeViewMU.CheckedNodes.Count > 0)
                {
                    if (e.Node.Checked == true)
                    {
                        e.Node.Checked = true;
                        CheckTreeNode(e.Node, true);
                    }
                    else
                    {
                        e.Node.Checked = false;
                        CheckTreeNode(e.Node, false);
                    }
                }
                  
               
               
            }

     效果图:

    简单明了。

  • 相关阅读:
    Caused by: java.util.MissingResourceException: Can't find bundle for base name javax.servlet.LocalStrings, locale zh_CN
    java调优
    idea的spring配置
    【从0到1学Web前端】javascript中的ajax对象(一) 分类: JavaScript 2015-06-24 10:18 754人阅读 评论(1) 收藏
    javascript中对条件判断语句的优化 分类: JavaScript 2015-06-07 09:54 832人阅读 评论(2) 收藏
    【从0到1学Web前端】CSS伪类和伪元素 分类: HTML+CSS 2015-06-02 22:29 1065人阅读 评论(0) 收藏
    django中url,静态文件,POST请求的配置 分类: Python 2015-06-01 17:00 789人阅读 评论(0) 收藏
    【从0到1学Web前端】CSS定位问题三(相对定位,绝对定位) 分类: HTML+CSS 2015-05-29 23:01 842人阅读 评论(0) 收藏
    Javascript图片预加载详解 分类: JavaScript HTML+CSS 2015-05-29 11:01 768人阅读 评论(0) 收藏
    【从0到1学Web前端】CSS定位问题二(float和display的使用) 分类: HTML+CSS 2015-05-28 22:03 812人阅读 评论(1) 收藏
  • 原文地址:https://www.cnblogs.com/Elgin/p/2057246.html
Copyright © 2020-2023  润新知