C# Umbraco:在用户控件中列出子节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1094610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:05:05  来源:igfitidea点击:

Umbraco: List Child Nodes in User Control

c#asp.netumbraco

提问by JGrimm

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.

我有一个用户控件,我需要根据 parentID 返回子节点。我能够获得 parentID,但不知道返回子节点的语法。

采纳答案by Tim Saunders

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