【问题标题】:Sitecore 8: Datasource value is null/empty in sublayout(.ascx)Sitecore 8:子布局(.ascx)中的数据源值为空/空
【发布时间】:2016-12-21 06:38:31
【问题描述】:

我试图通过后面的代码访问子布局的数据源值,我在数据源中得到空字符串。

以下是我在 sitecore 中的 Banner 子布局的屏幕截图以及如何在演示文稿中设置数据源值:

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

我做错了什么或者可能是什么问题?

【问题讨论】:

  • 是否所有相关项目都已发布(当前项目、数据源、..)?你能在网络数据库中验证吗..
  • 和题外话:如果你要投射,使用“as”而不是“is”;)
  • @Gatogordo - "as" 用于强制转换,"is" 用于检查继承。它们不可互换,除非您建议他使用“as”进行强制转换并将他的“if”语句替换为 null 检查,即检查“as 强制转换是否有效。
  • 尝试将您的上下文数据库更改为 master 并查看问题是否仍然存在?如果与出版无关。

标签: sitecore sitecore8


【解决方案1】:

如果你得到你的物品效果图怎么办:

RenderingReference[] renderings = currentItem.GetRenderings();

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

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

最后,

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多