如何从Sitecore内容编辑器中的自定义字段访问父项



通常,当我想访问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)?

我解决这个问题的方法是通过一个叫做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;
    }
}

最新更新