Code Expressions to Programmaticify Your Umbraco Site

I recently became aware of a construct in C# and ASP.NET that will solve all of your property binding problems. As a special case I’ll examine how the Umbraco Macro is buggy in this regard.

Current Methods of Programmatically Specifying Control Properties

There are a few ways that everyone knows how to specify control properties at runtime in ASP.NET, with varying success. There’s C#:

ctlMyControl.Property = "Property Value";

There’s the data binding expression:


These methods may not always work in your situation.  Find out when they won’t work and how to fix them by reading on…

Where these methods break down

ASP.NET Controls (henceforth, “controls”), like the Umbraco Macro control, really should be able to handle handle their properties being set at any stage during the ASP.NET page lifecycle that occur before before Render(). For one reason or another, a developer may not determine until, say, the “PreRender” phase, what the value of your control’s “Foo” property should be.  One example being that other controls that the developer is depending on to determine values of your control were also not written with the “update at any phase” ideology and have not yet come to a determinite state. The ASP.NET world is a mine field in this way.

The Umbraco Macro control is one of these controls. If you set a property, even in my experiences past the “PageInit” phase of the lifecycle, you’re wasting clock cycles, and now you’ll also have arthritis sooner. This is where the code expression builder comes in, and shines. Let’s look at code that doesn’t work:


protected override OnInit(EventArgs e){
	ctlSomeMacro.MacroAttributes["MyRuntimeSetProperty"] = "Runtime Value"; //  My hands hurt.

As early as OnInit, the Umbraco Macro control doesn’t care what you think the value should be.

So What’s a Code Expression Builder?

Great question! MSDN defines Expression Builders as objects that “parse declarative expressions and create code to retrieve values bound to a control property. In no-compile scenarios, an expression builder that supports a no-compile feature evaluates the expression during run time.” Control property – check. Declarative Expressions – check. Runtime – check. Other stuff – check.

Here’s the simplest CodeExpression code you can find everywhere on the web. It works beautifully too.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.Web.Compilation;
using System.Web.UI;

// This code is copy / paste-able and does everything, so antiarthritic.
// I believe it's been written once in the history of .NET
// and the namespace has been changed to protect the industrious.
namespace DelphicSage.UmbracoCMS.Helpers
	public class CodeExpressionBuilder : ExpressionBuilder
		public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,
		   object parsedData, ExpressionBuilderContext context)
			return new CodeSnippetExpression(entry.Expression);

Just drop it into your configuration—


And here’s code that works…


Note: The dollar sign and the “Code:” prefix are there so ASP.NET knows to look for an expression builder. Besides that, you can put any runtime computable expression inside of it! For instance, the following values will work





See some more examples by downloading the code.

« Prev Article
Next Article »