ParentId not recognized

4 posts, 0 answers
  1. Bob
    Bob avatar
    6 posts
    Member since:
    May 2014

    Posted 29 Mar Link to this post

    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.}

  2. Bob
    Bob avatar
    6 posts
    Member since:
    May 2014

    Posted 29 Mar Link to this post

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

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Bob
    Bob avatar
    6 posts
    Member since:
    May 2014

    Posted 29 Mar Link to this post

    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

  5. Nencho
    Admin
    Nencho avatar
    1458 posts

    Posted 01 Apr Link to this post

    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.
Back to Top