【问题标题】:How do I access the parent item from a custom field in the Sitecore Content Editor?如何从 Sitecore 内容编辑器中的自定义字段访问父项?
【发布时间】:2013-05-08 02:30:21
【问题描述】:

通常,当我想访问 Sitecore 中的当前项目时,我会通过 Sitecore.Context.Item。这适用于创建最终用户使用的工具,但不适用于 Sitecore 管理员使用的工具。如果我想在 Content Editor 本身中显示为自定义字段,Context.Item 是对 Content Editor 的引用,而不是对在编辑器中选择的节点的引用。

在大多数情况下,我可以通过使用ItemID 属性来解决这个问题,但如果我在现场有事件调度程序,它们将不再有权访问 ItemID。例如:

protected override void OnPreRender(EventArgs e)
{
    if (IsEvent)
    {
        // ItemID is defined here!
        Button live = FindControl(GetID(LiveButton)) as Button;
        if (live != null)
        {
            live.ServerProperties["Click"] = string.Format("{0}.LiveClicked", ID);
        }
    }
}

public void LiveClicked()
{
   // ItemID is blank here!
   DoSomething();
}

如何在我的侦听器中访问 ItemID(如上面的 LiveClicked)?

【问题讨论】:

  • 我意识到这不是自定义字段真正应该做的事情,但客户明确表示他希望内容编辑器的主体中具有某些功能。

标签: sitecore


【解决方案1】:

我解决它的方法是通过一个名为 ServerProperties 的东西,我在每个侦听器中调用了这个函数的等效函数。

private void SyncID()
{
    var live = FindControl(GetID(LiveButton)) as Button;
    if (live != null)
    {
        if(string.IsNullOrEmpty(ItemID))
        {
            ItemID = live.ServerProperties["owner_id"].ToString();
        }
        live.ServerProperties["owner_id"] = ItemID;
    }
}

【讨论】:

  • 我意识到这是代码异味。这让我觉得完全错误,但它解决了问题。如果有更好的答案,我很乐意接受并替换它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2016-07-08
  • 2015-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多