我有一个转发器,在该转发器上我绑定了3个项目的列表。 databind()被调用一次,我已经使用调试器进行了检查。我在这里有一些奇怪的行为,因为中继器似乎两次遍历了此项目列表。而不是3个项目,我看到中继器将所有内容绑定两次。

    /// <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++;
                }
            }
        }


中继器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>


关于如何解决的任何建议?
页面加载仅被调用一次,databind()仅被调用一次。

最佳答案

也许您在控件的父级之一中调用了DataBind方法。根据http://msdn.microsoft.com/en-us/library/w5e5992d.aspx(DataBind文档):


在服务器控件上调用时,此
方法解决所有数据绑定
服务器控件中的表达式和
在其任何子控件中。

关于c# - 数据绑定(bind)转发器使转发器两次加载所有项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3699037/

10-10 13:04