C# 从递归列表C Asp.net创建树视图
我想从recursiv列表中填充树视图, 我有一个项目,其中还包含项目列表等。 parentnodes的childnodes的数量和级别没有限制 下面是课堂:C# 从递归列表C Asp.net创建树视图,c#,asp.net,list,treeview,C#,Asp.net,List,Treeview,我想从recursiv列表中填充树视图, 我有一个项目,其中还包含项目列表等。 parentnodes的childnodes的数量和级别没有限制 下面是课堂: public class item { public int Id; public string texte; public List<item> listeItems; public string status;
public class item
{
public int Id;
public string texte;
public List<item> listeItems;
public string status;
public item()
{
this.listeItems = new List<item>();
}
}
我怎么能做到?
提前谢谢你 你需要写一些东西,比如:
void Populate(item i)
{
if (i == null)
return;
foreach (var child in i)
{
Populate(child);
}
i.Id = something;
i.texte = something;
i.status = something;
}
由于子节点的数量不受限制,更好的方法是使用字典,尽管它可能比列表慢。下面的实现是一种键值对数据结构,其中每个项都是键,其父项是值。希望能有帮助
using System.Collections;
使用System.Collections.Generic
类mytree
{
我想你需要父属性没有其他方法不需要父属性吗?
using System.Collections;
public static Dictionary<String, String> dict = new Dictionary<String, String>();
public void dictionaryadd(String key, String value)
{
dict.Add(key, value);
}
static List<int> GetKeysFromValue(Dictionary<string, string> dict, string value)
{
// Use LINQ to do a reverse dictionary lookup.
// Returns a 'List<T>' to account for the possibility
// of duplicate values.
return
(from item in dict
where item.Value.Equals(value)
select item.Key).ToList();
}
public String dictionaryout(String key)
{
string value;
if(dict.TryGetValue(key,out value))
return value;
else return String.Empty;
}
}
public HtmlGenericControl RenderMenu(List<item> nodes)
{
if (nodes == null)
return null;
var ul = new HtmlGenericControl("ul");
foreach (Node node in nodes)
{
var li = new HtmlGenericControl("li");
li.InnerText = node.texte;
if(node.listeItems != null)
{
li.Controls.Add(RenderMenu(node.listeItems));
}
ul.Controls.Add(li);
}
return ul;
}