2011-03-24 71 views
0

我有一個ASP.NET treeview控件,需要從一系列對象中遞歸地填充。至少,我需要分層顯示每個源和父節點的類別名稱和描述。爲此,我寫了一首歌我隱藏頁面如下:如何遞歸填充ASP.NET TreeView?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using CATALooK.Automation; 

namespace VBayUnMatched 
{ 
public partial class VBayCats : System.Web.UI.UserControl 
{ 
    private int id; 
    private CATALooK.Automation.SourceInfo source; 
    private CATALooK.Automation.CategoryInfo parent; 
    private string catID; 
    private string catName; 
    private string desc; 
    private string rawData; 
    private int advCatID; 
    private DateTime lastUpdated; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     CategoryController cc = new CategoryController(); 
     CategoryInfo ci = new CategoryInfo 
     (id,source,parent,catID,catName,desc,rawData,advCatID,lastUpdated); 
     //CATALooK.AdvCatInfoRemote[] acir = cc.getAdvancedCategories(); 
     TreeView trvUnMatchedCats = new TreeView(); 
     TreeNodeCollection tnColl = new TreeNodeCollection(); 
     TreeNode nodeSource = new TreeNode { Value = ToString(source) }; 
     TreeNode nodeParent = new TreeNode { Value = ToString(parent) }; 
     TreeNode nodeName = new TreeNode { Value = catName }; 
    } 

    private void PopulateTreeview() 
    { 


    } 

    private string ToString(SourceInfo source) 
    { 
     throw new NotImplementedException(); 
    } 

    private string ToString(CategoryInfo parent) 
    { 
     throw new NotImplementedException(); 
    } 

} 

}

如何使用遞歸分配父nodeParent,源nodeSource和catName到節點名稱?

非常感謝您的幫助和指導。

回答

1

最簡單(也是最可讀)的方法是創建一個實現IHierarchyData接口的類。這暴露了分層數據結構的節點,包括節點對象和描述節點特徵的一些屬性。實現IHierarchyData接口的對象可以包含在IHierarchicalEnumerable集合中,並由ASP.NET站點導航(如站點地圖)和數據源控件使用。

的一個偉大的樣本是found herehere you see如何在遞歸的方式

+0

真棒創建IHierarchyData。非常感謝!! – SidC 2011-03-24 22:51:04