获得每个子项,并显示它 [英] Get each child item and display it
问题描述
可以说我有一个父项目和Sitecore的CMS三个子项
Lets say I have a parent item and three child items in sitecore cms
Home
-ChildOne
-ChildTwo
-ChildThree
我要遍历所有子项,并使用他们的个人sublayouts显示出来。
I want to loop through all child items and display them using their individual sublayouts.
这是可能的,什么做我需要做实现这一目标。
Is this possible and what do I need to do to achieve this.
目前,我可以在一个占位符显示一个项目,我想使用一个中继器来做到这一点。
At the moment, I can display one item in one placeholder, I am thinking of using a repeater to do this.
什么是我最好的选择吗?这实际上是可能的?是否有任何抽奖背上的方法,你可以建议对我?
What are my best options? Is this actually possible? Are there any draw backs with the method you may suggest to me?
推荐答案
您可以使用&LT做到这一点Sitecore的。
You can accomplish this by using the <sc:sublayout ... />
control from Sitecore.
首先,你需要将每sublayout为那些访问数据源项目。 下面是示例code,我的话题博客一>
First, you need to make each sublayout for those access a DataSource item. Here is sample code I've blogged on the topic.
接下来,你需要重复过的孩子和他们绑定到sublayout控制,同时通过每个项目的数据源:
Next, you need to repeat over the children and bind them to the sublayout control while passing each item in as the DataSource:
前端:
<asp:Repeater ID="myRepeater" OnItemDataBound="myRepeater_ItemDataBound" runat="server">
<ItemTemplate>
<sc:sublayout ID="scSublayout" Path="path/to/your/sublayout/file.ascx" runat="server" />
</ItemTemplate>
</asp:Repeater>
code-背后:
// in the Page_Load
myRepeater.DataSource = homeItem.GetChildren();
myRepeater.DataBind();
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var scSublayout = e.Item.FindControl("scSublayout") as Sitecore.Web.UI.WebControls.Sublayout;
if (scSublayout != null)
{
scSublayout.DataSource = ((Sitecore.Data.Items.Item)e.Item.DataItem).ID.ToString();
}
}
}
这篇关于获得每个子项,并显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!