Try our conversational search powered by Generative AI!

Deane Barker
Jan 26, 2011
  5753
(1 votes)

Using Custom Attributes from the Property Control in your Custom Controls

When you write a custom property, you often end up writing a custom control for it too.  Then, when you call that property using the EPiServer Property control, it creates your internal (“inner”) control as part of its controls collection.  Your custom control might create other controls to actually render something.

For instance, when you use the PageLink property from the Property control, the actual control tree looks like this:

  • Property
    • PagePropertyReferenceControl
      • Hyperlink

What frustrated me is that I couldn’t figure out how to get a value “down” to the “lower” controls.  I wanted to be able to set random values on the EPiServer Property control (the first level), then access those from my custom control (the second level), to set values on the child controls it creates (the third level).

For instance, I wrote a custom property for “Gravatar Image.”  It displays the Gravatar image for the last editor of the current page.  You drop it on the page using the standard Property control (the first level in the structure shown above).

<EPiServer:Property PropertyName=”LastEditorImage” runat=”server”/>

This creates my custom GravatarImageControl as a child control (the second level).  That, in turn, creates an Image control (the third level).  Like this:

  • Property
    • GravatarImage
      • Image

However, Gravatar lets you send a size parameter in the URL when requesting the image (“?s=50” or something).  I wanted to be able to set this on the EPiServer Property control, and have it be passed down and eventually find its way onto the ImageUrl property of my Image control..

This is not that hard, it turns out.

From inside my GravatarImageControl, I can get a reference to the “owning” Property control:

Property owningPropertyControl = (Property)Parent;

On that, you’ll find an Attributes collection, from which you can reference anything.  So, I can insert my Property control like this:

<EPiServer:Property PropertyName=”LastEditorImage” PixelWidth=”50” runat=”server”/>

And from inside my GravatarImageControl, I can “pick up” that value and use it to affect the Image control I'm creating:

Property owningPropertyControl = (Property)Parent;
if (owningPropertyControl.Attributes["PixelSize"] != null)
{
    image.ImageUrl = image.ImageUrl + "?s=" + owningPropertyControl.Attributes["PixelSize"];
}

Thanks to Jacob Kahn for helping me figure this out.

Jan 26, 2011

Comments

Jan 27, 2011 04:22 PM

I used the AttributeSourceControl for this. See: https://www.coderesort.com/p/epicode/browser/MakingWaves.EPiImage/6.x/EPiImage/Properties/EPiImagePropertyControl.cs#L175

I guess it amounts to roughly the same effect though,

Please login to comment.
Latest blogs
Optimizely Forms - How to add extra data automatically into submission

Some words about Optimizely Forms Optimizely Forms is a built-in add-on by Optimizely development team that enables to create forms dynamically via...

Binh Nguyen Thi | Apr 29, 2024

Azure AI Language – Extractive Summarisation in Optimizely CMS

In this article, I demonstrate how extractive summarisation, provided by the Azure AI Language platform, can be leveraged to produce a set of summa...

Anil Patel | Apr 26, 2024 | Syndicated blog

Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog