Try our conversational search powered by Generative AI!

Deane Barker
Jan 26, 2011
  5762
(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
From Procrastination to Proficiency: Navigating Your Journey to Web Experimentation Certification

Hey there, Optimizely enthusiasts!   Join me in celebrating a milestone – I'm officially a certified web experimentation expert! It's an exhilarati...

Silvio Pacitto | May 17, 2024

GPT-4o Now Available for Optimizely via the AI-Assistant plugin!

I am excited to announce that GPT-4o is now available for Optimizely users through the Epicweb AI-Assistant integration. This means you can leverag...

Luc Gosso (MVP) | May 17, 2024 | Syndicated blog

The downside of being too fast

Today when I was tracking down some changes, I came across this commit comment Who wrote this? Me, almost 5 years ago. I did have a chuckle in my...

Quan Mai | May 17, 2024 | Syndicated blog

Optimizely Forms: Safeguarding Your Data

With the rise of cyber threats and privacy concerns, safeguarding sensitive information has become a top priority for businesses across all...

K Khan | May 16, 2024

The Experimentation Process

This blog is part of the series -   Unlocking the Power of Experimentation: A Marketer's Insight. Welcome back, to another insightful journey into...

Holly Quilter | May 16, 2024

Azure AI Language – Sentiment Analysis in Optimizely CMS

In the following article, I showcase how sentiment analysis, which is part of the Azure AI Language service, can be used to detect the sentiment of...

Anil Patel | May 15, 2024 | Syndicated blog