zoukankan      html  css  js  c++  java
  • 读取XML绑定TreeNode

    1 <asp:TreeView ID="treeview" OnClick="TreeViewCheckBox_Click()"  runat="server">
    2     </asp:TreeView>
    treeview.ShowCheckBoxes = TreeNodeTypes.All;
            treeview.ShowLines = true;
            treeview.ExpandDepth = 1;
            treeview.TreeNodeCheckChanged += new TreeNodeEventHandler(treeview_TreeNodeCheckChanged);
            if (!IsPostBack)
            {
                string url = "../../Xml/LayerConfig11.xml";
                url = Server.MapPath(url);
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(url);
    
                XmlNode fn = xDoc.DocumentElement;
                TreeNode tn = new TreeNode();
                tn.Text = fn.Attributes["Name"].Value;
                tn.ImageUrl = fn.Attributes["Icon"].Value;
                tn.SelectAction = TreeNodeSelectAction.None;
                treeview.Nodes.Add(tn);
    
                for (int i = 0; i < treeview.Nodes.Count; i++)
                {
                    if (treeview.Nodes[i].Checked)
                    {
                        treeview.Nodes[i].Parent.Checked = true;
                    }
                }
                XmlNodeList nodes = fn.ChildNodes;
                foreach (XmlNode item in nodes)
                {
    
                    if (item.Attributes["No"].Value == "06" || item.Attributes["No"].Value == "00")
                    {
                        TreeNode tn02 = new TreeNode();
                        tn02.Text = item.Attributes["Name"].Value;
                        tn02.ImageUrl = item.Attributes["Icon"].Value;
                        tn02.SelectAction = TreeNodeSelectAction.None;
                        tn.ChildNodes.Add(tn02);
                        AddNodes(tn02, item);
                    }
                }
            }
    1 void treeview_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    2     {
    3         TreeNode tn = e.Node;
    4         bool IsChecked = tn.Checked;
    5 
    6         CheckTreeNodeParent(tn, IsChecked);
    7 
    8         CheckTreeNodeChildren(tn, IsChecked);
    9     }
    treeview_TreeNodeCheckChanged事件
     1 /// <summary>
     2     /// 选中父类节点
     3     /// </summary>
     4     /// <param name="tn"></param>
     5     /// <param name="Checked"></param>
     6     private void CheckTreeNodeParent(TreeNode tn, bool Checked)
     7     {
     8         TreeNode ptn = tn.Parent;
     9 
    10         if (ptn != null)
    11         {
    12             if (Checked)
    13             {
    14                 ptn.Checked = Checked;
    15                 CheckTreeNodeParent(ptn, Checked);
    16             }
    17             else
    18             {
    19                 bool HasTreeNodeChecked = HasCheckedTreeNode(ptn);
    20                 if (!HasTreeNodeChecked)
    21                 {
    22                     ptn.Checked = Checked;
    23                     CheckTreeNodeParent(ptn, Checked);
    24                 }
    25             }
    26         }
    27     }
    选中父类节点
     1  private bool HasCheckedTreeNode(TreeNode tn)
     2     {
     3         bool IsChecked = false;
     4         if (tn != null)
     5         {
     6             TreeNodeCollection tns = tn.ChildNodes;
     7             if (tns != null)
     8             {
     9                 for (int i = 0; i < tns.Count; i++)
    10                 {
    11                     if (tns[i].Checked)
    12                     {
    13                         IsChecked = true;
    14                         break;
    15                     }
    16                     else
    17                     {
    18                         HasCheckedTreeNode(tns[i]);
    19                     }
    20                 }
    21             }
    22         }
    23 
    24         return IsChecked;
    25     }
    判断子节点中是否还有选中的节点
     1 private void CheckTreeNodeChildren(TreeNode tn, bool Checked)
     2     {
     3         TreeNodeCollection tns = tn.ChildNodes;
     4         if (tns != null)
     5         {
     6             for (int i = 0; i < tns.Count; i++)
     7             {
     8                 tns[i].Checked = Checked;
     9 
    10                 CheckTreeNodeChildren(tns[i], Checked);
    11             }
    12         }
    13     }
    选中子节点
     1 /// <summary>
     2     /// 递归绑定节点
     3     /// </summary>
     4     /// <param name="tnode">父级节点</param>
     5     /// <param name="xnode">源节点</param>
     6     private void AddNodes(TreeNode tnode, XmlNode xnode)
     7     {
     8         if (xnode.HasChildNodes)
     9         {
    10             foreach (XmlNode child in xnode.ChildNodes)
    11             {
    12                 TreeNode tn03 = new TreeNode();
    13                 tn03.Text = child.Attributes["Name"].Value;
    14                 tn03.ImageUrl = child.Attributes["Icon"].Value;
    15                 tn03.SelectAction = TreeNodeSelectAction.None;
    16                 tnode.ChildNodes.Add(tn03);
    17                 if (child.HasChildNodes)
    18                 {
    19                     AddNodes(tn03, child);
    20                 }
    21                 else
    22                 {
    23                     continue;
    24                 }
    25             }
    26         }
    27     }
    递归绑定节点
    得即高歌失即休,多愁多恨亦悠悠。今朝有酒今朝醉,明日愁来明日愁
  • 相关阅读:
    你喜欢使用eclipse+tomcat编程吗?!
    "ERR_GFX_D3D_INIT", GTA5-报错解决办法
    这样写JS的方式对吗?
    webui layout like desktop rich client
    2014年12月23日00:42:54——PS4
    2014年12月20日00:33:14-遮罩+进度条-extjs form.isvalid
    十分钟部署智能合约
    idea clion编译器
    parity 注记词
    go语言学习笔记
  • 原文地址:https://www.cnblogs.com/gotoschool/p/3592196.html
Copyright © 2011-2022 走看看