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.
You can probably do something clever with the Rules Engine, and this is probably how I would tackle this - possible change a rendering parameter using the Rules Engine based on the Placeholder and then do something in your code based on that...
But you can doing the following from code:
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;
}
}
As @jammykam says I would also encourage you to use Rules Engine to achieve a solution. And for that I will point you in the direction of this answer on Stackoverflow for a similar question(I think)
This should give you a good insight into the similar sort of scenario which the other user was facing.
Finally would encourage you to read Trayek's blog post on Changing placeholders with Rules
Hope the above helps and get you the solution that you are after.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With