Copied to clipboard

Flag this post as spam?

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


  • Paul Griffiths 370 posts 1021 karma points
    Feb 22, 2015 @ 00:21
    Paul Griffiths
    0

    Help listing child pages node name in DDL user control c#

    Hi all

    Sorry if this is a stupid question but I need help displaying child nodes in a drop down list that appears on a user control. I have used the following code and all though it is finding the 10 child pages in my drop down list it is displayig them all as umbraco.presentation.nodefactory.nodes. How can I display the actual node name? I'm new to this sorry

    using umbraco.presentation.nodeFactory; { public partial class ExampleUserControl : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) {

            //If you need a specific node based on ID use this method (where 123 = the desired node id)
            var specificNode = new Node(123);
    
            //To get the children as a Nodes collection use this method
            var childNodes = specificNode.Children;
    

    Var myList = new List

            //Iterating over nodes collection example
            foreach(var node in childNodes)
            {
                MyList.add(node.toString());
            }
    
            MyddL.DataSource = myList;
            MyddL.DataBind();
        }
    }
    

    }

    Thanks

    Paul

  • Alex Skrypnyk 6134 posts 23953 karma points MVP 8x admin c-trib
    Feb 22, 2015 @ 01:25
    Alex Skrypnyk
    0

    Hi Paul,

    You can use Dictionary or ListItems classes for creating datasource for dropdownlist.

        Dictionary<int, string> myDic = new Dictionary<int, string>();
        //Iterating over nodes collection example
        foreach(var node in childNodes)
        {
            nodes.Add(node.Id, node.Name);
        }
    
        MyddL.DataSource = myDic;
        MyddL.DataTextField = "Value";
        MyddL.DataValueField = "Key";
        MyddL.DataBind();
    

    Thanks,

    Alexander

  • Paul Griffiths 370 posts 1021 karma points
    Feb 22, 2015 @ 10:28
    Paul Griffiths
    0

    Hi Alexander,

    thanks for getting back.

    I have tried what you have suggested but i am getting compiler errors on nodes.Add(node.Id, node.Name); the only things that are available after the period on node are 4 methods and no properties such as id, name etc

    am i missing a namespace?

    thanks paul

  • Paul Griffiths 370 posts 1021 karma points
    Feb 22, 2015 @ 14:39
    Paul Griffiths
    100

    Hi

    I have found a way to make the node properties available I have changed the following from

    Var childNodes = specificNode.children

    To

    Var childNodes = specificNode.childrenAsList

    I was then able to access the node properties meaning I could add the name value to myList using the following

    MyList.add(node.Name);

    Thanks for your help

    Paul

Please Sign in or register to post replies

Write your reply to:

Draft