views:

1123

answers:

2

I have a user control in which I need to return child nodes based on parentID. I am able to get the parentID, but don't know the syntax for returning child nodes.

+4  A: 

Getting child nodes is pretty straightforward.

Not sure how far you are with your code so here's a complete example with the various options:

using umbraco.presentation.nodeFactory;

namespace cogworks.usercontrols
{
    public partial class ExampleUserControl : System.Web.UI.UserControl
    {
     protected void Page_Load(object sender, EventArgs e)
     {
      //If you just want the children of the current node use the following method
      var currentNode = Node.GetCurrent();

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

      //Iterating over nodes collection example
      foreach(var node in childNodes)
      {
       Response.Write(string.Format("{0}<br />", node.Name));
      }

      //To get the nodes as a datatable so you can use it for DataBinding use this method
      var childNodesAsDataTable = node.ChildrenAsTable();

        //Databind example
        GridViewOnPage.DataSource = childNodesAsDataTable;
        GridViewOnPage.DataBind();
     }
    }
}
Tim Saunders
A: 

Which version of umbraco are you using? because I have been trying to list children of a specific node for three days now, tried nodefactory, document etc, copy and pasted your code and it still doesn't work. HasChildren is always false, and I am sure it has nodes under it.

I should be able to get you an answer on this on Monday. Sorry, no access to my codebase till then. Let me know if you get your answer before then.
JGrimm
Have you double-checked that your child nodes have been published (by publishing the parent and checking the box to publish all children)?
David Conlisk