我在这里有一个非常基本的sitecore问题。我想遍历一组子对象(位置),并显示一些有关每个子对象的信息。

我正在使用ASP中继器进行迭代,正在页面上加载子级,并且正在尝试使用sc标记来显示信息-但我不会在页面上显示。

谁能帮我看看我在这里想念的东西吗?

后面的代码:

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


标记:

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


最后,在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();
                }
            }
        }
    }

最佳答案

您也可以这样:

页面加载:

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


标记:

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


在这种情况下,您不需要数据绑定事件。

10-06 10:59