开发者

Recursive display of sub objects in asp.net

开发者 https://www.devze.com 2023-03-20 20:45 出处:网络
My project has some \"MyObject\" and the MyObject have a property of List<MyObject>. I wou开发者_开发问答ld like to build a simple HTML nested unordered list to display the hierarchy for any spe

My project has some "MyObject" and the MyObject have a property of List<MyObject>. I wou开发者_开发问答ld like to build a simple HTML nested unordered list to display the hierarchy for any specified MyObject. A few concerns:

1) The number of children an total depth are unknown.

2) I would like to be able to limit the depth to X children.

3) The list items need to be able to contain any valid HTML and preferably asp.net controls e.g. a LinkButton

What is the best way to handle this? TreeViews? Nested ListViews? Sample code or a link to a tutorial would be much appreciated.


Your third concern is still unclear but the following should get you going. It uses a recursive function to read through an object and its children. In this example, the properties are being added to a simple bulleted list but it can be easily modified for other situations.

    public class MyObject
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public List<MyObject> Children { get; set; }
    }

    public void BuildTree(MyObject obj)
    {
        lit.Text += "<ul>";
        lit.Text += "<li>" + obj.Name + "- Age: " + obj.Age + "</li>";
        if (obj.Children != null)
        {
            foreach (MyObject objChild in obj.Children)
            {
                BuildTree(objChild);
            }
        }
        lit.Text += "</ul>";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        MyObject child1 = new MyObject { Name = "Joe", Age = 7 };
        MyObject child2 = new MyObject { Name = "Sally", Age = 6 };
        List<MyObject> children = new List<MyObject>();
        children.Add(child1);
        children.Add(child2);
        MyObject parent1 = new MyObject { Name = "Roger", Age = 36, Children = children };
        BuildTree(parent1);
    }


You may use TreeView like that

public static int maxDepth = 5;
public class MyObject
{
    public string Text;
    public string Value;
    public List<MyObject> Children;

    public MyObject(string text, string value)
    {
        Text = text;
        Value = value;
        Children = new List<MyObject>();
    }
    public MyObject(string text)
    {
        Text = text;
        Value = text;
        Children = new List<MyObject>();
    }
    public void AddToNode(TreeNode node, int depth)
    {
        TreeNode subNode = new TreeNode(Text, Value);
        node.ChildNodes.Add(subNode);
        if (depth < maxDepth)
        {
            for (int i = 0; i < Children.Count; i++)
            {
                Children[i].AddToNode(subNode, depth + 1);
            }
        }
    }
}
protected void Page_Load(object sender, EventArgs e)
{
    MyObject myObject;

    myObject = new MyObject("obj 1");
    myObject.Children.Add(new MyObject("obj 1.1"));
    myObject.Children.Add(new MyObject("obj 1.2"));
    myObject.Children[0].Children.Add(new MyObject("obj 1.1.1"));
    myObject.Children[0].Children.Add(new MyObject("obj 1.1.2"));
    myObject.Children[1].Children.Add(new MyObject("obj 1.2.1"));
    myObject.Children[1].Children.Add(new MyObject("obj 1.2.2"));

    treeView.Nodes.Add(new TreeNode("Root", "root"));
    myObject.AddToNode(treeView.Nodes[0], 0);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消