Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Deane Barker
Jan 26, 2011
  5751
(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
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

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog