1. 程式人生 > >TreeView遞迴生成樹(普通載入和快速載入、最快的是點一級載入子節點)

TreeView遞迴生成樹(普通載入和快速載入、最快的是點一級載入子節點)

 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();
        }

    }