Organizational Research By

Surprising Reserch Topic

recursive treeview in asp net


recursive treeview in asp net  using -'c#,asp.net,recursion,treeview'

I have an object of type list from which I wish to use to populate a treeview in asp.net c#.

Each object item has:

id | Name | ParentId


so for example:

id | Name     | ParentId
-------------------------
1  | Alice    | 0
2  | Bob      | 1
3  | Charlie  | 1
4  | David    | 2


In the above example, the parent would be Alice having two children Bob and Charlie. David is the child of Bob.

I have had many problems trying to dynamically populate the treeview recursively in c# ASP.NET

Does any one have a simple solution?

Btw: you can use People.Id, People.Name and People.ParentId to access the members since it is an object belonging to list.

I can post you my code so far (many attempts made) but not sure how useful it will be.
    

asked Sep 16, 2015 by tejas lakhani
0 votes
16 views



Related Hot Questions

3 Answers

0 votes

I think this should get you started. I created a MyObject class to mimic your object .

public class MyObject
{
    public int Id;
    public int ParentId;
    public string Name;
}

Here is a method to recursivley add tree view nodes based on the list.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List list = new List();
        list.Add(new MyObject(){Id=1, Name="Alice", ParentId=0});
        list.Add(new MyObject(){Id=2, Name="Bob", ParentId=1});
        list.Add(new MyObject(){Id=3, Name="Charlie", ParentId=1});
        list.Add(new MyObject(){Id=4, Name="David", ParentId=2});            

        BindTree(list, null);            
    }
}

private void BindTree(IEnumerable list, TreeNode parentNode)
{
    var nodes = list.Where(x => parentNode == null ? x.ParentId == 0 : x.ParentId == int.Parse(parentNode.Value));
    foreach (var node in nodes)
    {
        TreeNode newNode = new TreeNode(node.Name, node.Id.ToString());
        if (parentNode == null)
        {
            treeView1.Nodes.Add(newNode);
        }
        else
        {
            parentNode.ChildNodes.Add(newNode);
        }
        BindTree(list, newNode);
    }
}
answered Sep 16, 2015 by mannar kande
0 votes

You need to sort the data by the ParentId (as currently is). Add each node to the tree using the Id as the key. Then you can just run through the data adding nodes, you do not need recursion. You will need to get each parent node by it's key using the ParentId and add your next node to that parent.

There are more efficient ways of doing this, but lets start with the basics.

answered Sep 16, 2015 by r3tt
0 votes
    //In load for example
    if (!IsPostBack)
    {
            DataSet ds = new DataSet();
            ds = getRoles(); //function that gets data collection from db.

            tvRoles.Nodes.Clear();

            BindTree(ds, null); 
            tvRoles.DataBind();

    }       

    private void BindTree(DataSet ds, TreeNode parentNode)
    {
        DataRow[] ChildRows;
        if (parentNode == null)
        {
            string strExpr = "ParentId=0";
            ChildRows = ds.Tables[0].Select(strExpr);                    
        }
        else
        {
            string strExpr = "ParentId=" + parentNode.Value.ToString();
            ChildRows = ds.Tables[0].Select(strExpr); 
        }   
        foreach (DataRow dr in ChildRows)
        {
            TreeNode newNode = new TreeNode(dr["Name"].ToString(), dr["Id"].ToString());
            if (parentNode == null)
            {
                tvRoles.Nodes.Add(newNode);
            }
            else
            {
                parentNode.ChildNodes.Add(newNode);
            }
            BindTree(ds, newNode);
        }
    }
answered Sep 16, 2015 by badhwar.rohit

...