TreeView递归生成树(普通加载和快速加载、最快的是点一级加载子节点)

时间:2021-12-04 12:39:56

 1、普通递归法(速度慢)  

 在TreeView中添加选中节点的背景样式<SelectedNodeStyle BackColor="Silver" />

     <asp:TreeView ID="TreeView1" runat="server" ShowLines="True">
          <SelectedNodeStyle BackColor="Silver" />
      </asp:TreeView>

 

    /// <summary>
    /// 生成树
    /// </summary>
    private void AddTree(string ParentID, TreeNode pNode)
    {
        DataSet  ds = org.GetDataI(); //获取所有数据
        DataView dvTree = new DataView(ds.Tables[0]); //把ds放到DataView中
        //过滤ParentOrgID,得到当前的所有子节点  
        dvTree.RowFilter = "[ParentOrgID]   =   '" + ParentID + "'";
        dvTree.Sort = "OrderBy";
        foreach (DataRowView Row in dvTree)
        {
            TreeNode node = new TreeNode();
            if (pNode == null)
            {         //添加根节点  
                node.Text = Row["Name"].ToString();
                node.Value = Row["OrgID"].ToString();
                node.ImageUrl = this.SkinFolder + @"images\Org\Dept1.gif";
                node.NavigateUrl = "SerachRecordInfo.aspx?OrgID=" + node.Value + "";
                node.Target = "mainFrame";
                //node.NavigateUrl.NavigateUrl =Request.Url.LocalPath +"?OrgID=" + Row["OrgID"].ToString();
                node.Expanded = false;
                 //TreeView1.Style.Add("background-color", "Silver");
               
                TreeView1.Nodes.Add(node);
                AddTree(Row["OrgID"].ToString(), node);         //再次递归  

            }
            else
            {       //添加当前节点的子节点  

                node.Text = Row["Name"].ToString();
                node.Value = Row["OrgID"].ToString();
                node.ImageUrl = this.SkinFolder + @"images\Org\Dept2.gif";
                node.NavigateUrl = "SerachRecordInfo.aspx?OrgID=" + node.Value + "";
                node.Target = "mainFrame";
                node.Expanded = false;
                pNode.ChildNodes.Add(node);
                AddTree(Row["OrgID"].ToString(), node);         //再次递归  
            }

        }
    }

 

 2、优化后的代码(加载速度快了太多)

   在page_load外面定义一个 DataSet用于存需要加载的全部数据

  在page_load的 if(!IsPostBack){ds = index.Fill();AddTree("-1", (TreeNode)null);}//fill方法是我写好的获取DataSet的一个方法,在index类中,我这里直接调用

public void AddTree(string ParentID, TreeNode pNode)
    {
        if (ds.Tables.Count > 0)
        {
            DataView dvTree = new DataView(ds.Tables[0]);
            //过滤ParentOrgID,得到当前的所有子节点  
            dvTree.RowFilter = "[IndexParentID]   =   '" + ParentID + "' and [StatusFlag]='1'";
            foreach (DataRowView Row in dvTree)
            {
                TreeNode node = new TreeNode();
                if (pNode == null)
                {         //添加根节点  
                    node.Text = Row["IndexName"].ToString();
                    node.Value = Row["IndexID"].ToString();
                    TreeView1.Nodes.Add(node);
                    AddTree(Row["IndexID"].ToString(), node);         //再次递归  
                }
                else
                {       //添加当前节点的子节点  
                    node.Text = Row["IndexName"].ToString();
                    node.Value = Row["IndexID"].ToString();
                    pNode.ChildNodes.Add(node);
                    AddTree(Row["IndexID"].ToString(), node);         //再次递归  
                }
            }
            dvTree.Dispose();
            ds.Dispose();
        }

    }