This is a migrated thread and some comments may be shown as answers.

ParentId not recognized

3 Answers 87 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Bob
Top achievements
Rank 1
Bob asked on 29 Mar 2016, 03:45 PM

Hi collegues,

I would like to bind my inherited TreeView to a custom object list. 
It should automatically make the nodes based on parent-child relationships.

But at the databind (line 10.) I get a runtime error "Object of type TreeViewItem does not have a ParentId property.".
Which is a lie, as you can see :)

When i comment line 7. It also happens with the other properties(DataTextField and DataNavigateUrlField), but this is the first to run into.

Do you have any idea what is going wrong here?

Kind regards,

Bob Gladon

01.public class NavigateTreeView : RadTreeView
02.{
03.    protected void NavigateTreeView_Load(object sender, EventArgs e)
04.    {
05.       this.DataTextField = "Name";
06.       this.DataFieldID = "ItemId";
07.       this.DataFieldParentID = "ParentId";
08.       this.DataNavigateUrlField = "UrlPath";
09.       this.DataSource = GetTreeViewItems();
10.       DataBind();
11.    }
12.
13.    private List<TreeViewItems> GetTreeViewItems()
14.    {
15.        //Some implementation
16.    }
17.}
18.
19.public class TreeViewItem
20.{
21.    public TreeViewItemType Type;
22.    public int ParentId;
23.    public int? ItemId;
24.    public string Name;
25.    public string UrlPath;
26.}

3 Answers, 1 is accepted

Sort by
0
Bob
Top achievements
Rank 1
answered on 29 Mar 2016, 04:25 PM

Also tried it without inheritance, so i guess it will be something simple:

<telerik:RadTreeView runat="server" ID="menuTreeview" />

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
 
    menuTreeview.DataFieldParentID = "ParentId";
    menuTreeview.DataFieldID = "ItemId";
    menuTreeview.DataTextField = "Name";
    menuTreeview.DataNavigateUrlField = "UrlPath";
    menuTreeview.DataSource = GetNavItems();
    menuTreeview.DataBind();
}
 
private List<TreeViewItem> GetNavItems()
{
    var list = new List<TreeViewItem>() {
        new TreeViewItem
        {
            Name = "RootCluster",
            Type = TreeViewItemType.Cluster,
            ItemId = 1,
            ParentId = 0,
        },
        new TreeViewItem
        {
            Name = "Level 1 Cluster 1",
            Type = TreeViewItemType.Cluster,
            ItemId = 2,
            ParentId = 1,
        },          
        new TreeViewItem
        {
            Name = "Level 2 Cluster 1.1",
            Type = TreeViewItemType.Cluster,
            ItemId = 3,
            ParentId = 2,
        },
        new TreeViewItem
        {
            Name = "Level 2 Cluster 1 Page 1",
            Type = TreeViewItemType.AspNetPage,
            ItemId = 4,
            ParentId = 2,
        },
        new TreeViewItem
        {
            Name = "Level 3 Cluster 1.1 Page 1",
            Type = TreeViewItemType.AspNetPage,
            ItemId = 5,
            ParentId = 3,
        },
        new TreeViewItem
        {
            Name = "Level 1 Page 1",
            Type = TreeViewItemType.AspNetPage,
            ItemId = 6,
            ParentId = 1,
        }
    };
 
 
    return list;
}

0
Bob
Top achievements
Rank 1
answered on 29 Mar 2016, 05:10 PM

Well the solution is pretty easy.

For anyone who has the same problem:

The RadTreeView uses get accessors of the object to get the value, so the solution is:

public class TreeViewItem
{
    public TreeViewItemType Type { get; set; }
    public int ParentId { get; set; }
    public int ItemId { get; set; }
    public string Name { get; set; }
    public string UrlPath { get; set; }
}

Kind regards,

Bob Gladon

0
Nencho
Telerik team
answered on 01 Apr 2016, 10:38 AM
Hello Bob,

Thank you for sharing your implementation and solution with the community.

Regards,
Nencho
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
TreeView
Asked by
Bob
Top achievements
Rank 1
Answers by
Bob
Top achievements
Rank 1
Nencho
Telerik team
Share this question
or