我有一个转发器,我用3个项目绑定一个列表。 databind()被调用一次,我使用调试器检查了这一点。我在这里有一些奇怪的行为,因为Repeater似乎两次遍历这个项目列表。而不是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++;
}
}
}
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>
有关如何解决此问题的任何建议? 页面加载只调用一次,databind()只调用一次。
答案 0 :(得分:4)
也许您在其中一个控件的父级中调用了DataBind方法。根据{{3}}(DataBind docs):
在服务器控件上调用时,这个 方法解析所有数据绑定 服务器控件中的表达式 在任何儿童控制中。
答案 1 :(得分:2)
通常,在!Page.IsPostBack
中绑定内容时,您应该检查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
}
}
这可能会解决您当前的问题。