2010-09-13 16 views
1

我有一箇中繼器,我用三個項目綁定一個列表。數據綁定()被調用一次,我已經使用調試器進行了檢查。我在這裏有一些奇怪的行爲,因爲Repeater似乎兩次遍歷這個項目列表。而不是3項,我看到中繼器將所有東西都綁定了兩次。數據綁定中繼器使Repeater加載所有項目兩次

/// <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()只調用一次。

+0

粘貼你的轉發器的html以及 – 2010-09-13 09:14:22

+0

*而不是3個項目,我看到轉發器綁定一切兩次* - 你是什麼意思?你的轉發器中有6個元素? – 2010-09-13 09:38:15

+0

我在我的轉發器中獲得3個元素,然後我看到 - 使用調試器 - 控件的代碼被調用。在此之後,中繼器再次重複3個項目,所以項目列表由3個項目組成,但是它們被處理兩次...... – Younes 2010-09-13 10:10:46

回答

2

也許你在其中一個控件的父項中調用了DataBind方法。據http://msdn.microsoft.com/en-us/library/w5e5992d.aspx(的DataBind文檔):

當叫上一個服務器控件,這 方法解決,在任何其子控件的服務器控件和 所有數據綁定 表達式。

+0

我無法找到任何控件的父母中的任何一個正在調用的DataBind方法。我發現這個奇怪的行爲:/。 – Younes 2010-09-13 10:09:44

+0

@Younes包含您的中繼器的用戶控件可能位於自動執行數據綁定的控件的內部,因此您可能看不到明確的對.DataBind()的調用。 – 2013-08-27 15:07:47

2

通常,在Page_Load中綁定東西時,您應該檢查!Page.IsPostBack

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

這可能會解決您目前的問題。

+0

頁面加載只調用一次,所以不是這樣。但我會添加這個只是爲了確保。 – Younes 2010-09-13 09:34:44

相關問題