惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
Google Developers Blog
T
Threat Research - Cisco Blogs
Cyberwarzone
Cyberwarzone
罗磊的独立博客
S
Security Affairs
D
Docker
Microsoft Azure Blog
Microsoft Azure Blog
G
GRAHAM CLULEY
W
WeLiveSecurity
博客园 - Franky
C
Check Point Blog
The Last Watchdog
The Last Watchdog
F
Full Disclosure
Security Latest
Security Latest
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
AWS News Blog
AWS News Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Troy Hunt's Blog
S
Secure Thoughts
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Privacy & Cybersecurity Law Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
P
Privacy International News Feed
S
Schneier on Security
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
aimingoo的专栏
aimingoo的专栏
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Recent Announcements
Recent Announcements
IT之家
IT之家
D
DataBreaches.Net
U
Unit 42
博客园 - 聂微东
H
Hacker News: Front Page
GbyAI
GbyAI
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
TaoSecurity Blog
TaoSecurity Blog
Google Online Security Blog
Google Online Security Blog
T
Threatpost
博客园 - 叶小钗
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
K
Kaspersky official blog

博客园 - Burt

软件项目研发的设计流程 访问IIS元数据库失败解决方法(转) 经典SQL语句大全 js 比较2日期大小 - Burt - 博客园 合并表格,并实现对datatable的group by 功能 - Burt 时间段查询 left join on sql 日期格式转换(convert用法)(转) 分页存储过程 刷新后保持页面滚动条位置C#&VB(转) - Burt - 博客园 Dropdownlist样式设置 - Burt - 博客园 读取天气预报所产生乱码解决方法MiniSite.Weather.print("Wealth") 40种网页常用小技巧(javascript) 页面图象鼠标操作放大和制作图片热点 无法将类型login_aspx转换为System.Web.UI.WebControl c# 发邮件(转摘) [没有相关源的行] 访问被拒绝 水晶报表制作 页面自动刷新以及广告程序得Javascript代码
TreeView的遍历(转)
Burt · 2009-03-26 · via 博客园 - Burt

  权限分配当然连接数据了,就会用到Treeview这个控件。它的节点ID肯定绑到数据库表里的ID了,怎么得到 Treeview里的全部节点的ID。

for (int i = 0; i < TreeView1.Nodes.Count; i++)
{
       if (TreeView1.Nodes[i].Selected == true)
        {
            Response.Write(TreeView1.Nodes[i].Value.ToString());
          }
}
foreach (TreeNode tree in TreeView1.Nodes)
     {

      if (tree.Checked == true)
           {
              Response.Write(tree.Value.ToString());
    }

}这两个只能取到根节点的节点ID。子节点怎么办?

从网上查了下,基本上就是这样做:

在TreeView查找某一节点,通常有两种方法,一种是递归的,一种不是递归,但都是深度优先算法。其中,非递归方法效率高些,而递归算法要简洁一些。


第一种,递归算法,代码如下:

private TreeNode FindNode( TreeNode tnParent, string strValue )

    {

        if( tnParent == null ) return null;

        if( tnParent.Text == strValue ) return tnParent;

        TreeNode tnRet = null;

        foreach( TreeNode tn in tnParent.Nodes )

        {

            tnRet = FindNode( tn, strValue );

            if( tnRet != null ) break;

        }

        return tnRet;

    }

第二种,非递归算法,代码如下:

private TreeNode FindNode( TreeNode tnParent, string strValue )

    {

        if( tnParent == null ) return null;

        if( tnParent.Text == strValue ) return tnParent;

        else if( tnParent.Nodes.Count == 0 ) return null;

        TreeNode tnCurrent, tnCurrentPar;

        //Init node

        tnCurrentPar = tnParent;

        tnCurrent = tnCurrentPar.FirstNode;

        while( tnCurrent != null && tnCurrent != tnParent )

        {

            while( tnCurrent != null )

            {

                if( tnCurrent.Text == strValue ) return tnCurrent;

                else if( tnCurrent.Nodes.Count > 0 )

                {

                    //Go into the deepest node in current sub-path

                    tnCurrentPar = tnCurrent;

                    tnCurrent = tnCurrent.FirstNode;

                }

                else if( tnCurrent != tnCurrentPar.LastNode )

                {

                    //Goto next sible node

                    tnCurrent = tnCurrent.NextNode;

                }

                else

                    break;

            }

            //Go back to parent node till its has next sible node

            while( tnCurrent != tnParent && tnCurrent == tnCurrentPar.LastNode )

            {

                tnCurrent = tnCurrentPar;

                tnCurrentPar = tnCurrentPar.Parent;

            }

            //Goto next sible node

            if( tnCurrent != tnParent )

                tnCurrent = tnCurrent.NextNode;

        }

        return null;

    }

       程序调用,如下:

TreeNode tnRet = null;

        foreach( TreeNode tn in yourTreeView.Nodes )

        {

            tnRet = FindNode( tn, yourValue );

            if( tnRet != null ) break;

        }

===================================================================================或:

        private void button1_Click_1(object sender, EventArgs e)
        {
            TreeView tree;
            foreach (TreeNode node in tree.Nodes)
            {
                GetNode(node);
            }
        }
        void GetNode(TreeNode node)
        {
            foreach (TreeNode n in node.Nodes)
            {
                Console.WriteLine(n.Name);
                GetNode(n);
            }
        }

我试了下,方法是对的,但是具体代码不太对,运行出错。哎,只好自己写,下面是我写的,测试过了,可以运行。写得不好,有点麻烦,用的递归

public void GetNode(TreeNodeCollection tc)
    {
        foreach (TreeNode TNode in tc)
        {
            if (TNode.Parent == null)
            {
                Response.Write(TNode.Value.ToString());
            }
            if (TNode.ChildNodes.Count == 0)
            {
                Response.Write(TNode.Value.ToString());
            }
            else
            {
                GetNode(TNode.ChildNodes);
               
            }
          
        }
       
    }
调用:

TreeNodeCollection tc = TreeView1.Nodes;
        GetNode(tc);