5

Is there a way to know which placeholder a sublayout is sitting in through code?

Scenario: I would like for a sublayout to appear a little different if it is in Placeholder B as opposed to Placeholder A. I could do this with sublayout parameters but I am trying to make it so an author doesn't have to set up a value and rather the sublayout is just smart enough to know what context it is being used in.

4

2 回答 2

7

您可能可以使用规则引擎做一些聪明的事情,这可能就是我将如何解决这个问题 - 可能使用基于占位符的规则引擎更改渲染参数,然后基于此在您的代码中执行某些操作......

但是您可以从代码中执行以下操作:

Sublayout sublayout = this.Parent as Sublayout;
if (sublayout != null)
{
    Placeholder placeholder = sublayout.Parent as Placeholder;
    if (placeholder != null)
    {
        // name of the container placeholder
        string placeHolderKey = placeholder.Key;
        // full path of nested placeholders
        string placeHolderContextKey = placeholder.ContextKey;
    }
}
于 2013-11-12T20:32:32.587 回答
3

正如@jammykam 所说,我也鼓励您使用规则引擎来实现解决方案。为此,我将在 Stackoverflow 上为您指出类似问题的答案方向(我认为)

这应该可以让您很好地了解其他用户所面临的类似情况。

最后鼓励您阅读Trayek关于使用规则更改占位符博客文章

希望以上内容对您有所帮助,并为您提供所需的解决方案。

于 2013-11-13T01:11:35.740 回答