2016-12-21 17 views
0

我想通過後面的代碼訪問sublayout的數據源值,並且我在數據源中獲得空字符串。Sitecore 8:數據源值爲空/空的子圖層(.ascx)

下面是我的旗幟sublayout在Sitecore的屏幕截圖和數據源值是如何在演示設置:

Banner Sublayout with Datasource location and template set

Banner sublayout added to presentation and datasource set to an item

下面是我使用的檢索數據源代碼:

using Sitecore.Data.Items; 
    using Sitecore.Web.UI.WebControls; 
    using System; 
    using System.Web.UI; 

    public partial class Banner : UserControl 
    { 
     private Item _dataSource = null; 
     public Item DataSource 
     { 
      get 
      { 
       if (_dataSource == null) 
        if (Parent is Sublayout) 
        { 
         _dataSource = Sitecore.Context.Database.GetItem(((Sublayout)Parent).DataSource); 
        } 
       return _dataSource; 
      } 
     } 
     private void Page_Load(object sender, EventArgs e) 
     { 
      var test = DataSource; 
      Item a = Sitecore.Context.Item; 
     } 
    } 

我在做什麼不對或有什麼問題?

+1

公佈所有相關項目(當前項目,數據源,..)?你可以在網絡數據庫驗證.. – Gatogordo

+0

和偏題:使用「as」,而不是「is」,如果你打算施展;) – Gatogordo

+0

@Gatogordo - 「as」用於投射,「is」用於檢查繼承。除非你建議他使用「as」進行演員表,並用空格檢查替換他的「if」語句,即檢查「演員表是否正常工作」, – DougCouto

回答

0

如果您得到您的項目效果圖:

RenderingReference[] renderings = currentItem.GetRenderings(); 

然後,得到你想要使用的渲染:

var rendering = renderings.Where(r => r.RenderingItem.ID != yourRenderingID) 

最後,

rendering.Settings.DataSource //This is the datasource id