C# – Simple Sitecore ASP Repeater – Accessing Fields

asp.netc++sitecore

I have a pretty basic sitecore question here. I want to iterate over a group of child objects (locations), and display some information about each.

I'm using an ASP repeater to do the iteration, I'm getting the children on the page load, and I'm trying to use the sc tags to display the information – but I'm not getting to show up on the page.

Can anyone help me out to see what I'm missing here?

Code behind:

protected void Page_Load(object sender, EventArgs e)
    {
        var item = Sitecore.Context.Item;

        var children = new List<Sitecore.Data.Items.Item>();
        foreach (var child in item.GetChildren())
        {
            children.Add((Sitecore.Data.Items.Item)child);
        }

        LocationsRpt.DataSource = children;
        LocationsRpt.DataBind();
    }

Markup:

<asp:Repeater runat="server" ID="LocationsRpt" OnItemDataBound="LocationsRptItemDataBound">
    <ItemTemplate>
        <div class="InnerContentSec clearfix">
            <div id="AboutSolar" class="AboutSolar clearfix">
                <div class="items">
                <h3>
                    <sc:Text runat="server" ID="Title"/>
                </h3>
                <div class="LocationBlock clearfix">
                <div class="ImgSec">
                    <sc:Image id="Image" runat="server" Width="185" Height="107" />
                </div>
                <div class="DescSec">
                    <p><sc:Text ID="ShortDescription" runat="server" /></p>
                </div>
                </div>
                </div>
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>

And, finally on DataItemBound

protected void LocationsRptItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var subItem = e.Item.DataItem as Item;  

            if (subItem != null)
            {
                var appTitle = e.Item.FindControl("Title") as Sitecore.Web.UI.WebControls.Text;

                if (appTitle != null)
                {
                    appTitle.DataSource = subItem.ID.ToString();
                    appTitle.Field = "Application Title";
                    appTitle.DataBind();
                }

                var appImage = e.Item.FindControl("Image") as Sitecore.Web.UI.WebControls.Image;

                if (appImage != null)
                {
                    appImage.DataSource = subItem.ID.ToString();
                    appImage.Field = "Location Image";
                    appImage.DataBind();
                }

                var shortDescription = e.Item.FindControl("ShortDescription") as Sitecore.Web.UI.WebControls.Text;

                if (shortDescription != null)
                {
                    shortDescription.DataSource = subItem.ID.ToString();
                    shortDescription.Field = "Short Description";
                    shortDescription.DataBind();
                }
            }
        }
    }

Best Solution

You could also do it like so:

Pageload:

protected void Page_Load(object sender, EventArgs e)
{
    LocationsRpt.DataSource = Sitecore.Context.Item.GetChildren();
    LocationsRpt.DataBind();
}

Markup:

<asp:Repeater runat="server" ID="LocationsRpt">
    <ItemTemplate>
        <div class="InnerContentSec clearfix">
            <div id="AboutSolar" class="AboutSolar clearfix">
                <div class="items">
                <h3>
                    <sc:Text runat="server" ID="Title" Item="<%# Container.DataItem %>"/>
                </h3>
                <div class="LocationBlock clearfix">
                <div class="ImgSec">
                    <sc:Image id="Image" runat="server" Width="185" Height="107"  Item="<%# Container.DataItem %>" />
                </div>
                <div class="DescSec">
                    <p><sc:Text ID="ShortDescription" runat="server"  Item="<%# Container.DataItem %>" /></p>
                </div>
                </div>
                </div>
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>

In this case you don't need the databound event.

Related Question