Try our conversational search powered by Generative AI!

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

Optimizely Data Platform Visitor Groups now supports multiple instances

The module V2.0 now supports multiple Optimizely Data Platform instances, allowing personalized content based on real-time segments and profile dat...

Andrew Markham | May 15, 2024 | Syndicated blog

IP block for edit and admin in DXP

Why IP-blocking edit/admin? A hacker can try to guess user names and passwords to gain access to your site. This risk can be minimized in a couple ...

Daniel Ovaska | May 15, 2024

Do not upgrade to EPiServer.CMS.Core 12.21.4 without reading this!

Todays update of EPiServer.CMS.Core 12.21.4 alters default sort order in an unexpected way, when you are working with muligple languages and have...

Tomas Hensrud Gulla | May 14, 2024 | Syndicated blog