Embedded code blocks in Ascx macros are great – I like to do Ascx’es in Umbraco UI

Embedded code blocks (in Ascx, Aspx and masterpage files) are a remnant from the past. From the dark old ages when Asp ruled the planet. Msdn says it’s still in AspNet “primarily to preserve backward compatibility”. Msdn page

But lately .NET people seem to appreciate it more. And with MVC now it looks like it will really have it’s resurrection.

Here’s a sample of child nodes navigation Ascx in Umbraco – no other code than this is necessary:

<%@ Import Namespace="umbraco.presentation.nodeFactory" %>
<%  foreach (Node c in Node.GetCurrent().Children) {%>
<li><a href="<%=umbraco.library.NiceUrl(c.Id)%>">
<%=c.Name %></a></li>
<% } %>

This syntax looks really php’ish / asp’ish. It is indeed a nice way to separate markup from logic and still being able to do loops and other dynamic stuff.

To have the same possibilities for creating Ascx’es as we have with Xslts in Umbraco UI really makes great sense to me. I would love a customcontrols folder in the developer section where creating a new one would add a macro automatically. For code editing its really the same requirements as we already have with templates (masterpages).

In Visual Studio editing Ascx with code blocks are a very pleasant experience. Complete control over the markup. No more forgotten close tags. Full Intellisense. And Ctrl+K – Ctrl+D to format everything nicely.

Limitations and when a codebehind or a class is necessary

One thing is that it does not allow functions, and thus recursion is not possible to achieve (feel welcome to prove me wrong on that). Edit: Yup wrong. Here’s a post by Elad Ossadon about how it can be achieved.

So for a complete “site structure under current” map you can not use plain “Get children of children” logic, instead you’ll have to keep that in a codebehind – with a simple call from the ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SampleNavigation.ascx.cs" Inherits="SampleNavigation" %>
<%@ Import Namespace="umbraco.presentation.nodeFactory" %>
<%=SampleRecursiveNodes(Node.GetCurrent()) %>

And here’s the code behind. If you like to have it somewhere else, in a global class or even call a Orm-created function from your Ascx that works great.

public partial class SampleNavigation : System.Web.UI.UserControl

public static string SampleRecursiveNodes(Node n)
System.Text.StringBuilder sb = new System.Text.StringBuilder();

string liTemplate = "<li><a href='{0}'>{1}</a>{2}</li>\r\n";
foreach (Node c in n.Children)
string childNodes = "";
if (c.Children.Count > 0)
sb.AppendFormat(liTemplate, umbraco.library.NiceUrl(c.Id), c.Name,childNodes);

return sb.ToString();


Although, using Xml iteration instead it is indeed possible do achieve complete site strucure aswell.

I had some problems with Linq syntax in a code block aswell, will research further on that one.

The coming Razor syntax seems just to be a tiny justification to embedded code blocks. Using @ instead of <%= %> but that also wont allow functions and hence no recursive calls to be made. (As far as I can see.)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s