Hello World!

asp.net使用treeview控件,递归加载

protected void Page_Load(object sender, EventArgs e)
{
MyTree t1 = new MyTree { ID = 1, Name = "adimn1", ParentId = 0 };
MyTree t2 = new MyTree { ID = 2, Name = "adimn11", ParentId = 1 };
MyTree t3 = new MyTree { ID = 3, Name = "adimn12", ParentId = 1 };
MyTree t4 = new MyTree { ID = 4, Name = "adimn13", ParentId = 1 };
MyTree t5 = new MyTree { ID = 5, Name = "adimn5", ParentId = 0 };
MyTree t6 = new MyTree { ID = 6, Name = "adimn51", ParentId = 5 };
MyTree t7 = new MyTree { ID = 7, Name = "adimn52", ParentId = 5 };
MyTree t8 = new MyTree { ID = 8, Name = "adimn8", ParentId = 0 };
List<MyTree> list = new List<MyTree> {
t1,t2,t3,t4,t5,t6,t7,t8
};
BindTreeView(TreeView1, true, false, new MyTree { Name = "管理首页", ID = 0 }, list);
}

 

public void BindTreeView(TreeView tview, bool isExpanded, bool isLink, MyTree root, List<MyTree> childs)
{
tview.Nodes.Clear();
TreeNode rootNode = new TreeNode(root.Name, root.ID.ToString());
rootNode.Expanded = isExpanded;
if (isLink)
{
rootNode.NavigateUrl = root.Navigator;
rootNode.Target = root.Target;
rootNode.ImageUrl = root.ImageUrl;
}
tview.Nodes.Add(rootNode);
this.CreateChildNodes(rootNode, childs, isExpanded, isLink);

 

}

public void CreateChildNodes(TreeNode parentNode, List<MyTree> trees, bool isExpanded, bool isLink)
{
var _mytrees = trees.Where(o => o.ParentId.ToString() == parentNode.Value);
foreach (MyTree t in _mytrees)
{
///创建新节点
TreeNode node = new TreeNode();
///设置节点的属性
node.Text = t.Name;
node.Value = t.ID.ToString();
node.Expanded = isExpanded;

if (isLink == true)
{
///格式化后台管理页面的链接地址
node.NavigateUrl = t.Navigator;
node.Target = "MainFrame";
}
parentNode.ChildNodes.Add(node);
///递归调用,创建其他节点
CreateChildNodes(node, trees, isExpanded, isLink);
}
}

posted @ 2012-06-19 13:24  世界万物  阅读(281)  评论(0编辑  收藏  举报
Bye World!