Get each child item and display it

Lets say I have a parent item and three child items in sitecore cms

Home
  -ChildOne 
  -ChildTwo
  -ChildThree

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?

Answer

You can accomplish this by using the <sc:sublayout ... /> control from Sitecore.

First, you need to make each sublayout for those access a DataSource item. Here is sample code I’ve blogged on the topic.

Next, you need to repeat over the children and bind them to the sublayout control while passing each item in as the DataSource:

Front-end:

<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-behind:

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