欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Asp.net treeview實(shí)現(xiàn)無限級(jí)樹實(shí)現(xiàn)代碼

 更新時(shí)間:2009年09月17日 14:14:43   作者:  
最近研究了一下treeview,發(fā)現(xiàn)有兩種實(shí)現(xiàn)無限級(jí)樹的方法,文字不想多寫,直入主題。

先看看效果圖:

先看看數(shù)據(jù)庫表的設(shè)計(jì),數(shù)據(jù)表主要包括ID,Name,ParentID這三項(xiàng),其中ID是主鍵,ParentID對(duì)應(yīng)節(jié)點(diǎn)的父節(jié)點(diǎn):


方法一:用遞歸遍歷數(shù)據(jù),并將節(jié)點(diǎn)逐個(gè)添加到treeview中去。
1.先進(jìn)行數(shù)據(jù)庫連接和數(shù)據(jù)的讀取,并將根節(jié)點(diǎn)先添加進(jìn)treeview中,并利用遞歸getTreeView()實(shí)現(xiàn)數(shù)據(jù)的遍歷和添加:

復(fù)制代碼 代碼如下:

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

2.數(shù)據(jù)遍歷的遞歸函數(shù),比較簡(jiǎn)單就不多說了。
復(fù)制代碼 代碼如下:

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

方法二:用TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)事件響應(yīng)來逐個(gè)讀取子節(jié)點(diǎn)。
1.第一步基本和上一方法的第一步一致,只是要將節(jié)點(diǎn)的設(shè)置為不展開。
復(fù)制代碼 代碼如下:

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

2.再改寫TreeView1_TreeNodePopulate(),根據(jù)鼠標(biāo)的點(diǎn)擊得到該節(jié)點(diǎn)的ID,然后根據(jù)該ID進(jìn)行數(shù)據(jù)的讀取,將其下的子節(jié)點(diǎn)讀出。
復(fù)制代碼 代碼如下:

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

以上兩種方法都可以實(shí)現(xiàn)無限級(jí)分類,不過第一種方法顯然更好一些,第二種方法不可以實(shí)現(xiàn)全部展開功能,而第一種可以。

相關(guān)文章

最新評(píng)論