zoukankan      html  css  js  c++  java
  • ASP.NET通过递归添加树(Treeview)

    先来看看效果,基本上就是这样的。

    所谓树,无非就是2点,第一个:根节点,第二:叶子节点,其中叶子节点中还可能有叶子节点,但是根节点始终只有一个。

    下面贴上 各部分的代码

    1.PAGE_LOAD载入事件。

        protected void Page_Load(object sender, EventArgs e)
        {
            BindTree(); //绑定树
        }
    

    2.BindTree方法。

        private void BindTree()
        {
          DataSet  ds = new DataSet(); //这里的DS里不存在数据,具体情况请大家自己具体考虑,修改代码
            
            TreeView1.Nodes.Clear(); 
    
            AddTree("-1", (TreeNode)null,ds);
            TreeView1.Nodes[0].Expanded = true;
        }
    

    3.AddTree方法,递归添加树。

     
    /*其中ds对应的是一个实体类Model,里面的IndexName,IndexID等,都是对应着数据库里的相应字段所以我们在使用这个方法时前,必须先得到相对应的有具体意义的DataSet*/
    
     /// <summary>
        /// 递归树
        /// </summary>
        /// <param name="ParentID">当前节点父节点ID</param>
        /// <param name="pNode"></param>
        /// <param name="ds"></param>
    private void AddTree(string ParentID, TreeNode pNode,DataSet ds)
        {
            DataView dvTree = new DataView(ds.Tables[0]); //用DataView来模拟DataTable,因为DV是虚拟的表,所以可以提高效率
            foreach (DataRowView Row in dvTree)
            {
                TreeNode node = new TreeNode();
                if (pNode == null)
                {         //添加根节点   
                    node.Text = Row["IndexName"].ToString();
                    node.Value = Row["IndexID"].ToString(); 
               
                    if (Row["IndexClass"].ToString() == "0")
                    {
                        node.NavigateUrl = "IndexInfo1.aspx?IndexID=" + node.Value;
                    }
                    else
                    {
                        node.NavigateUrl = "IndexInfo2.aspx?IndexID=" + node.Value;
                    }
                    
                    node.Target = "mainFrame";
                    node.Expanded = false;
                    TreeView1.Nodes.Add(node);  //添加根节点
                    AddTree(Row["IndexID"].ToString(), node,ds);         //递归,添加子节点,这段递归是必须的,不然只会添加根节点,而进不到下面的else语句,pNode在TreeView第二次执行以后都是不为空的,所以会一直跳到执行ELSE语句里的内容。   
                    
                }
                else
                {       //添加当前节点的子节点   
    
                    node.Text = Row["IndexName"].ToString();
                    node.Value = Row["IndexID"].ToString();
                    node.ToolTip = node.Text;   
                    if (Row["IndexClass"].ToString() == "0" || Row["IndexParentID"].ToString() == "-1")
                    {
                        
                        node.NavigateUrl = "IndexInfo1.aspx?IndexID=" + node.Value;
                    }
                    else
                    {
                       
                        node.NavigateUrl = "IndexInfo2.aspx?IndexID=" + node.Value;
                    }
                    
                    node.Target = "Frame1"; //某一个节点指向的一个IFRAME,里面可以是这个节点下的具体内容。
                    //node.NavigateUrl = Request.Url.LocalPath + "?OrgID=" + Row["OrgID"].ToString();
                    node.Expanded = false;
                    pNode.ChildNodes.Add(node); //pNode为父节点,把Node作为子节点添加进去
                    AddTree(Row["IndexID"].ToString(), node,ds);         //递归添加子节点 
                }
    
            }
    
        }
    

      

     

  • 相关阅读:
    oracle中 start with .. connect by prior.. 用法简介
    Java中com.jcraft.jsch.ChannelSftp讲解
    linux修改系统时间和linux查看时区、修改时区的方法
    map.containsKey
    Struts2中struts.multipart.maxSize配置
    oracle定时器job的使用
    java的System.getProperty()方法可以获取的值
    夜间模式的开启与关闭,父模板的制作
    开始Flask项目
    完成登录与注册页面的前端
  • 原文地址:https://www.cnblogs.com/kmsfan/p/3612958.html
Copyright © 2011-2022 走看看