先看看效果图:
先看看数据库表的设计,数据表主要包括ID,Name,ParentID这三项,其中ID是主键,ParentID对应节点的父节点:
方法一:用递归遍历数据,并将节点逐个添加到treeview中去。
1.先进行数据库连接和数据的读取,并将根节点先添加进treeview中,并利用递归getTreeView()实现数据的遍历和添加:
复制代码 代码如下:
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.数据遍历的递归函数,比较简单就不多说了。
复制代码 代码如下:
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)事件响应来逐个读取子节点。
1.第一步基本和上一方法的第一步一致,只是要将节点的设置为不展开。
复制代码 代码如下:
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(),根据鼠标的点击得到该节点的ID,然后根据该ID进行数据的读取,将其下的子节点读出。
复制代码 代码如下:
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);
}
}
以上两种方法都可以实现无限级分类,不过第一种方法显然更好一些,第二种方法不可以实现全部展开功能,而第一种可以。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]