views:

103

answers:

2

I have a repeater on which I bind a list with 3 items. The databind() is called once, I have checked this using the debugger. I have some strange behavior here because the Repeater seems to walk through this list of items twice. Instead of the 3 items, I see the repeater bind everything twice.

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        Sitecore.Data.Items.Item contextItem = Sitecore.Context.Item;
        Sitecore.Data.Fields.MultilistField thisSnippets = contextItem.Fields["snippets"];
        List<Item>thisSnippetItems = thisSnippets.GetItems().ToList<Item>();

        if (thisSnippetItems.Count > 0)
        {
            rptListRenderer.DataSource = thisSnippetItems;
            rptListRenderer.DataBind();
        }
    }


    /// <summary>
    /// Handles the ItemDataBound event of the rptListRenderer control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterItemEventArgs"/> instance containing the event data.</param>
    private void rptListRenderer_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            Item dataItem = (Item)e.Item.DataItem;

            System.Web.UI.WebControls.PlaceHolder phLiContent = (System.Web.UI.WebControls.PlaceHolder)e.Item.FindControl("phLiContent");

            if (phLiContent != null)
            {
                Sitecore.Data.Items.DeviceItem listItemDevice = Sitecore.Context.Database.Resources.Devices["List item"];
                RenderingReference[] renderings = dataItem.Visualization.GetRenderings(listItemDevice, false);

                foreach (RenderingReference rendering in renderings)
                {
                    string strDataSource = dataItem.ID.ToString();
                    rendering.Settings.DataSource = strDataSource;

                    Sublayout thisControl = (Sublayout)rendering.RenderingItem.GetControl(rendering.Settings);           

                    if (blockCounter == 0)
                    {
                        thisControl.Parameters = "class=snippetColHomeFirst";
                    }
                    else
                    {
                        thisControl.Parameters = "class=";
                    }

                    phLiContent.Controls.Add(thisControl);

                    blockCounter++;
                }
            }
        }

repeater html:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="hpSnippetList.ascx.cs" Inherits="Snippets.Sublayouts.hpSnippetList" %>
<%@ Register TagPrefix="sc" Namespace="Sitecore.Web.UI.WebControls" Assembly="Sitecore.Kernel" %>
<asp:repeater id="rptListRenderer" runat="server" EnableTheming="false" EnableViewState="false">
 <itemtemplate>
   <asp:placeholder id="phLiContent" runat="server" />
 </itemtemplate>
</asp:repeater>

Any suggestions on how this can be solved? Page load is called only once, databind() is only called once.

+2  A: 

Normally, you should check for !Page.IsPostBack when binding things in Page_Load.

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Sitecore.Data.Items.Item contextItem = Sitecore.Context.Item;
        // ... the rest of the code
    }
}

This might solve your current problem.

Dan Dumitru
Page load is only called once, so it's not this. But I will add this one just to be sure.
Younes
+1  A: 

Perhaps you called DataBind method in one of the control's parents. According to http://msdn.microsoft.com/en-us/library/w5e5992d.aspx (DataBind docs):

When called on a server control, this method resolves all data-binding expressions in the server control and in any of its child controls.

Simon
I couldn't find any DataBind methods being called in one of the control's parents. I find this strange behavior :/.
Younes