Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET > Treeview > Treeview pouplating with repeated nodes when binding pages
RadControls for ASP.NET are no longer supported (see this page for reference). In case you have inquiries about the Telerik ASP.NET AJAX controls, post them in the pertinent ASP.NET AJAX forums.

Not answered Treeview pouplating with repeated nodes when binding pages

Feed from this thread
  • Fareed avatar

    Posted on Jun 7, 2011 (permalink)

    Hi,

    I have a situation where i would need to bind all the pages of sitefinity application into treeview with all nodes and childnodes. However I am getting some problem while implementing. I am using following code to get the pages of sitefinity using fluentapi
    protected void Page_Load(object sender, EventArgs e)
    {
    .....
    ......
    ......
     IQueryable<PageNode> pageNodes;
    var fluent = App.WorkWith();
                fluent.Pages().GetManager().Provider.SuppressSecurityChecks = true;
                pageNodes = fluent.Pages()
                    //get front end pages
                   .LocatedIn(Telerik.Sitefinity.Fluent.Pages.PageLocation.Frontend)
                   .Where(pN => pN.Page.Status == ContentLifecycleStatus.Live)

                   .Get();
                PopulateTreeViewControl();
    }

    Now getting page details in pageNodes, I m calling method PopulateTreeViewControl() which will bind the treeview with parent pages and child pages
    Following  is the code of popuplating treeview

    private void PopulateTreeViewControl()
            {
                TreeNode parentNode = null;
                string pnode = string.Empty;

                foreach (PageNode pageNode in pageNodes.Distinct())
                {
                    pnode = pageNode.Parent.Title.ToString();
                   parentNode = new TreeNode(pnode, pageNode.Parent.Id.ToString());
                    foreach (PageNode pagenode1 in pageNode.Parent.Nodes.Distinct())
                    {
                        TreeNode childNode = new TreeNode(pagenode1.Page.Title, pagenode1.Page.Id.ToString());
                        parentNode.ChildNodes.Add(childNode);
                    }
                    
                    if (!(TreeView1.Page.Items.Equals(parentNode)))
                    {
                        TreeView1.Nodes.Add(parentNode);
                    }

                    parentNode.Collapse();
                }
            }
    With this above code, my treeview is getting popuplated with parent and child pages but the problem is that i am getting nodes repeated in my treeview. Had tried several methods to get actual treeview but no success.
    Please check the code and let me know that is it correct method or where i am lacking to get actual result. Or is there any alternative ways to get popuplated treeview.
    Your help and suggesstion will be very useful. Awaiting for your response.

    Thanks
    Fareed Khan


  • Ivan Dimitrov Ivan Dimitrov admin's avatar

    Posted on Jun 13, 2011 (permalink)

    Hi Fareed,


    Why don't you populate the control with SiteMapDataSource items and inside ItemDataBound check whether the pageNode is navigable or not?

    <telerik:RadTreeView runat="server" ID="TreeView1" DataSourceID="SiteMap1"></telerik:RadTreeView>

    <asp:SiteMapDataSource runat="server" ID="SiteMap1" ShowStartingNode="false" />


    What I suppose is that you are calling PopulateTreeViewControl() twice or multiple times TreeView1.Nodes.Add(parentNode)  /  parentNode.ChildNodes.Add(childNode);; for the same node.

    Also you can write another function that calls itself recursively.

    sample

    public IList<PageNode> GetPages(Guid parentId)
           {
               IList<PageNode> listofCmsPages = new List<PageNode>();
               var manager = PageManager.GetManager();
               var allpages = manager.GetPageNodes().Where(pn => pn.Parent.Id == parentId);
               foreach (PageNode p in allpages)
               {
                   listofCmsPages.Add(p);
                   if (p.Nodes.Count > 0)
                   {
                       foreach (PageNode nn in p.Nodes)
                       {
                           listofCmsPages.Add(nn);
                           GetPages(p.Id);
                       }
                   }
               }
               return listofCmsPages;
      
           }


    All the best,
    Ivan Dimitrov
    the Telerik team

    Consider using RadControls for ASP.NET AJAX (built on top of the ASP.NET AJAX framework) as a replacement for the Telerik ASP.NET Classic controls, See the product support lifecycle here.

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET > Treeview > Treeview pouplating with repeated nodes when binding pages