Block Data from Master Template's Code Behind

Vote:
 

This may be really simple, but I'm trying to access a property within a block from the code behind in a master template (web forms).

For page properties, I've used something like ((PageBase)Page).CurrentPage.Property["propertyname"]...

Is there something similar to get a specific property from a block? (BlockData)Block).CurrentData.Property["x"]... ?

#142229
Dec 05, 2015 0:31
Vote:
 

Hi,

First I would recommend you to cast the CurrentPage object to its real type, or at least to a base class. However, this is how you access the block, given that the block is a property on CurrentPage:

((PageBase)Page).CurrentPage.Property["YourBlockPropertyName"]

This is usually how you would do it:

// a base class that always has the block property
var basePage = ((PageBase)Page).CurrentPage as PageBase;

if (basePage != null)
{
    var value = basePage.YourBlockProperty.PropertyOnBlock;
}
#142238
Edited, Dec 07, 2015 9:42
Vote:
 

Hmm can't edit my previous post, or at least not the code. The first snippet should have been:

((PageBase)Page).CurrentPage["YourBlockProperty.PropertyOnBlock"]

And unfortunately I named the base class the same as the base class for pages :/ So I hope this makes it more clear:

// MetaDataPage is a base class that always has the block property
var metaPage = ((PageBase)Page).CurrentPage as MetaDataPage;
 
if (metaPage != null)
{
    var value = metaPage.YourBlockProperty.PropertyOnBlock;
}
#142239
Dec 07, 2015 9:47
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.