I have a block in the content area of another block.
How can I access one of the properties of the parent block from the view?
I am using episerver 7.5
I would not recommend that a block tries to access information outside its context.
Could it be possible for you to push the content from the wrapping View by using RenderSettings (WebForms) or AdditionalViewData (MVC)?
Can you describe more your case? Maybe something interesting comes in mind..
I am trying to achive the same thing, why isn't it recommended?
This will only generate an error due to the ParentLink is not refering to an object implementing IContent nor IContentData...
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var data = contentRepository.Get<IContentData>(((IContent)this).ParentLink);
"this" represents the block context, and of course it is pretty obvious it will throw an exception but is there a way i can do this?
ParentLink in this case is the parent in the content tree, not the information about where it's placed in a ContentArea.
Thanks for your answer Alf.
So in other words, there is no way to get the parent relativley from the current block (if it's decendant of course).Is there no relation between the child and the parent exposed on the object then?
No "official" way that I have seen and what I've seen so far I would not recommend that you try to look for information outside the scope of the block. Mainly because there are multiple ways to use a block:
And some other ways that I might miss right now.
You should look at your block as isolated unit of stuff that may render some other stuff. I would agree with Alf, if this block needs information from hosting party (another's block content area, page content area, etc) then host is responsible for providing that data and not block accessing this data and pulling in from somewhere. This sounds more like violating responsobility boundaries of components..
Thanks for the input, I totally agree with you both after looking it through a second time, I didn't really take the time to understand the true nature of blocks.I solved it in another way instead, which doens't violate the responsibility, I also got the chance to refactor my previous implementation.
It's technically possible via ContentAreaRenderer (google for content area extensibility), but not recommended.