先看看效果圖:

先看看數據庫表的設計,數據表主要包括ID,Name,ParentID這三項,其中ID是主鍵,ParentID對應節點的父節點:

復制代碼 代碼如下:
 
protected void Page_Load(object sender, EventArgs e) 
{ 
if (!Page.IsPostBack) 
{ 
TreeNode nodeCategory ; 
connection conn = new connection(); 
List<Category> category = conn.getCategory(); 
Stack<Category> storeCategory = new Stack<Category>(); 
storeCategory.Push(category[0]); 
nodeCategory = new TreeNode(category[0].Name.Trim(), category[0].Id.Trim()); 
TreeView1.Nodes.Add(nodeCategory); 
getTreeView(storeCategory, category, nodeCategory); 
} 
} 
復制代碼 代碼如下:
 
public void getTreeView(Stack<Category> categoryStack,List<Category> categoryList,TreeNode e) 
{ 
Category tmp; 
if(categoryStack.Count>0) 
{ 
tmp=categoryStack.Pop(); 
for(int i=0;i<categoryList.Count;i++) 
if(categoryList[i].ParentId.Trim()==tmp.Id.Trim()) 
{ 
categoryStack.Push(categoryList[i]); 
TreeNode childNote = new TreeNode(categoryList[i].Name.Trim(), categoryList[i].Id.Trim()); 
e.ChildNodes.Add(childNote); 
getTreeView(categoryStack, categoryList, childNote); 
} 
} 
} 
復制代碼 代碼如下:
 
protected void Page_Load(object sender, EventArgs e) 
{ 
if (!Page.IsPostBack) 
{ 
TreeNode nodeCategory ; 
connection conn = new connection(); 
List<Category> category = conn.getCategory(); 
nodeCategory = new TreeNode(category[0].Name.Trim(), category[0].Id.Trim()); 
nodeCategory.PopulateOnDemand = true; 
nodeCategory.Collapse(); 
nodeCategory.NavigateUrl = "http://blog.csdn.net/longlongago2000"; 
nodeCategory.Target = "_blank"; 
TreeView1.Nodes.Add(nodeCategory); 
} 
} 
復制代碼 代碼如下:
 
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e) 
{ 
int categoryID = Int32.Parse(e.Node.Value); 
connection conn = new connection(); 
List<Category> category = conn.getCategory(); 
foreach (Category tmp in category) 
{ 
if (categoryID.ToString() == tmp.ParentId.Trim()) 
{ 
TreeNode childNote = new TreeNode(tmp.Name.Trim(), tmp.Id.Trim()); 
foreach (Category cate in category) 
{ 
if (tmp.Id.Trim() == cate.ParentId.Trim()) 
{ 
childNote.PopulateOnDemand = true; 
childNote.Collapse(); 
break; 
} 
else 
childNote.Expand(); 
} 
childNote.NavigateUrl ="http://blog.csdn.net/longlongago2000" ; 
childNote.Target = "_blank"; 
e.Node.ChildNodes.Add(childNote); 
} 
} 
 
  | 
新聞熱點
疑難解答
圖片精選