Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lachlann 344 posts 626 karma points
    Nov 15, 2010 @ 18:38
    Lachlann
    0

    Bind Node children to Treeview

    Hi All

     

    I am trying to write a user control that has access to the content tree. Does any one have a good way of binding the content tree to a treeview ?

    I have tried writing a recursive function to walk the tree but im not getting any where any help would be great!

     

    L

  • Eduardo 106 posts 130 karma points
    Nov 15, 2010 @ 20:55
    Eduardo
    0

    Hi L,

    How about using Linq2Umbraco?

    Video tutorial:

    http://vimeo.com/9788833

    HTH.

    Sincere regards,
    Eduardo Macho

  • Lachlann 344 posts 626 karma points
    Nov 16, 2010 @ 17:45
    Lachlann
    0

    Hey Thanks Eduardo,

     

    Had a quick look at Linq2Umbraco, but not entirly sure how it will help I am quite new to .Net.

    I have a TreeView control added to the .ascx file and I am trying to populate it using the umbraco content tree. My control requires that the user is able to choose a node form within the content tree.

     

    any suggestions?

     

    Thanks

     

    L

  • Eduardo 106 posts 130 karma points
    Nov 16, 2010 @ 18:37
    Eduardo
    1

    Hi L,

    Binding the content tree to a treeview

    I presume your ascx file contains a line of code like the next.

     <asp:TreeView ID="TreeView1" 
    SkinId="Bookstoree
    ExpandDepth="3"
    MaxDataBindDepth="3"
    runat="server" />

    http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/navigation/treeview.aspx

    1. We need to retrieve all the nodes in the Umbraco Content XML and format them.

    This can be done using a special function within Umbraco Business Logic.

            umbraco.presentation.nodeFactory.Node node = new umbraco.presentation.nodeFactory.Node(-1);
            umbraco.presentation.nodeFactory.Nodes childrenNodes = node.Children;

    2. Bind on the Page_Load event of the control all the data from the Umbraco Content XML to the tree control.

    protected void Page_Load(object sender, EventArgs e)


            umbraco.presentation.nodeFactory.Node node = new umbraco.presentation.nodeFactory.Node(-1);
            umbraco.presentation.nodeFactory.Nodes childrenNodes = node.Children;

            foreach (umbraco.presentation.nodeFactory.Node n in childrenNodes)
            {
                Treeview1.Nodes.Add(new TreeNode(n.Name, n.Id.ToString()));
                Treeview1.DataBind();
            }
    }

    HTH.

    Sincere regards,
    Eduardo

  • Lachlann 344 posts 626 karma points
    Nov 17, 2010 @ 16:33
    Lachlann
    0

    Thanks Eduardo,

    That helped alot, in the end I managed to get my recursive function working so that the whole content tree could be stepped and then bound to a tree view.

     

            private void initTree()
            {
                myTree.Nodes.Clear();
                myTree.ExpandDepth = 1;
                myTree.Nodes.Add(walkTree(new Node(-1)));
                myTree.DataBind();
            }



            private TreeNode mapumbNode2treeNode(Node nodeIn){
                var tempNode = new TreeNode()
                    {
                       Text = nodeIn.Name,
                        Value = nodeIn.Id.ToString()
                    };

                     var thisDocType = DocumentType.GetByAlias(nodeIn.NodeTypeAlias);
                     try
                       {
                         var docTypeImage = thisDocType.IconUrl;
                       }
                    catch (Exception e) { return new TreeNode(); }
                return tempNode;
            }

            private TreeNode walkTree(Node nodeIn){
                TreeNode treeNode = mapumbNode2treeNode(nodeIn);
                if (nodeIn.Children.Count > 0)
                {
                    foreach (Node node in nodeIn.Children)
                    {
                        treeNode.ChildNodes.Add(walkTree(node));
                    }
                }
                return treeNode;
            }

     

    thanks again for your help/

  • Eduardo 106 posts 130 karma points
    Nov 17, 2010 @ 16:37
    Eduardo
    0

    Hi Lachlan,

    Glad to hear from you. You are Welcome!.

    If my answer helped you, please mark it as the solution.

    Sincere regards,
    Eduardo Macho

Please Sign in or register to post replies

Write your reply to:

Draft