gpt4 book ai didi

c# - 数据绑定(bind)中继器使中继器加载所有项目两次

转载 作者:行者123 更新时间:2023-12-02 11:53:05 25 4
gpt4 key购买 nike

我有一个转发器,我在上面绑定(bind)了一个包含 3 个项目的列表。 databind() 被调用一次,我已经使用调试器检查了这一点。我在这里有一些奇怪的行为,因为中继器似乎两次遍历这个项目列表。我看到中继器将所有内容绑定(bind)两次,而不是 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 (数据绑定(bind)文档):

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com