中继器项数据都使用源项数据进行数据

时间:2012-01-24 17:24:35

标签: c# asp.net repeater sitecore

好的,我有一个转发器:

<asp:Repeater ID="myRepeater"  OnItemDataBound="myRepeater_ItemDataBound" runat="server">

    <HeaderTemplate>
    </HeaderTemplate>

    <ItemTemplate>

    <sc:sublayout ID="scSublayout" Path="/Components/Spot/BlurbSpot.ascx" runat="server" />

    </ItemTemplate>

</asp:Repeater>

数据绑定如下:

myRepeater.DataSource = Sitecore.Context.Item.GetChildren();
myRepeater.DataBind();

我的项目数据绑定获取数据源并将其应用于sc:sublayout:

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

它显示两个项目,但这两个项目显示来自第二个数据源的数据。调试我可以看到它找到了两者,但第一个循环的原始值由于某种原因被写出来。

我想要的是同时显示的项目,以及第一个被循环的项目,而不是将其变量写入。

@Mark Ursino

public partial class BlurbSpotSublayout : Logic.BaseSublayout
{

    public static string Blurb_Spot_Heading;
    public static string Blurb_Spot_SubHeading;
    public static string Blurb_Spot_Abstract;

    private void Page_Load(object sender, EventArgs e) 
    {
        if (DataSource != null)
        {
            Blurb_Spot_Heading = BlurbSpot.GetInstance(DataSource.Paths.FullPath.ToString()).Heading;
            Blurb_Spot_SubHeading = BlurbSpot.GetInstance(DataSource.Paths.FullPath.ToString()).SubHeading;
            Blurb_Spot_Abstract = BlurbSpot.GetInstance(DataSource.Paths.FullPath.ToString()).BlurbAbstract;
        }

    } 

前端是:

<div ID="Div3" runat="server"><%=Blurb_Spot_SubHeading%></div>
<div ID="Div1" runat="server"><%=Blurb_Spot_Heading %></div>
<div ID="Div2" runat="server"><%=Blurb_Spot_Abstract%></div>

然后我通过以下方式将项目传递给后面的课程:

Database webDB = Sitecore.Configuration.Factory.GetDatabase("web");
Sitecore.Data.Items.Item item;
this.item = webDB.Items[item];

GetInstance()。标题执行此操作:

public string Heading
    {
        get
        {
            if (item != null)
            {

                    Sitecore.Data.Fields.Field field = item.Fields["BlurbSpot_Content_Header"];

                    if (field != null)
                    {
                        return field.Value;
                    }
                    else
                    {
                        return _heading; // returns default pixel width if BlurbSpot_Heading is not define, or is null
                    }
            }

            return _heading;

        }

        set
        {
            _heading = value;
        }
    }

1 个答案:

答案 0 :(得分:2)

问题是您的字符串被静态声明为:

public static string Blurb_Spot_Heading;
public static string Blurb_Spot_SubHeading;
public static string Blurb_Spot_Abstract;

因为它们是静态的,所以它们在用户控件的所有实例之间共享。将它们声明为:

public string Blurb_Spot_Heading;
public string Blurb_Spot_SubHeading;
public string Blurb_Spot_Abstract;

试一试。