Deane Barker
Jan 26, 2011
  5781
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024