1

My project has some "MyObject" and the MyObject have a property of List<MyObject>. I would 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.

2
  • 1
    Can you give more description of what you're trying to display? Do you want to display a hierarchical list of all objects and their properties? How do the asp.net controls relate? Commented Jul 15, 2011 at 19:55
  • I want a tree list of the display name for my objects and the names may end up being LinkButtons so I can have them post back and say for instance re-build the list from the object I clicked on the name for. Commented Jul 15, 2011 at 20:41

2 Answers 2

2

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);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I didn't even think of building an actual HTML list in a literal lol.
This was the basis for the way I set it up. Thank you.
1

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

1 Comment

What if I wanted to make the node text a link, or better yet an asp:LinkButton? Is that possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.