Sitecore locating sublayouts on your webpage

This is a followup from the post, Get Sitecore placeholders and rendering hierarchy from a Sitecore item, where I adress one of the difficulties when working with pages in Sitecore, that they tend to get very complex as one page may contain 10-30 sublayouts and renderings.
Is is very easy to loose track of which sublayout is placed where, especially if you use nested or dynamic placeholders on your pages.

My colleagues Laust Skat Nielsen and Jannik Nilsson came up with this solution to write out the name of the sublayout in the rendered HTML when in debug mode.

Their method adds a comment to the HTML when a control is rendered:

Showing the path to the control

Showing the path to the control

It is very simple to do. On PreRender, simply go through the control collection and inject the path before the usercontrol is rendered. Place the following code in the codebehind of your Layout (.aspx) file.

protected void Page_PreRender(object sender, EventArgs e)
  if (HttpContext.Current.IsDebuggingEnabled)

protected void InjectControlPaths(ControlCollection controlCollection)
  foreach (Control control in controlCollection)
    if (control is UserControl)
        control.Controls.AddAt(0, new LiteralControl("<!-- Control path: " + (control as UserControl).AppRelativeVirtualPath + " -->"));
      catch (Exception exception)
        Sitecore.Diagnostics.Log.Debug("Failed to inject comment into " + (control as UserControl).AppRelativeVirtualPath);

Notice how the control tree is only rendered when in debugging mode. A very simple and clever solution to a common nuisance. Thanks guys.


About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, Sitecore 6, Sitecore 7, Sitecore 8 and tagged , , , , , . Bookmark the permalink.

5 Responses to Sitecore locating sublayouts on your webpage

  1. Nice one Brian. Something worth pointing out is that this means that your UserControls shouldn’t have syntax inside otherwise Asp.Net will complain that it can’t modify the controls collection. It’s better to use the syntax then with a “DataBind()” in the Codebehind.

    I wonder if there’s a way we can hook into one of Sitecore’s pipelines instead so it supports both Webforms and MVC?

    Best wishes, Steve

  2. briancaos says:

    Yes that is right. As Laust explained to me, the controls using <#= #> syntax with throw an exception, hence the try…catch in the code.
    And you are right, for MVC projects we need another approach.

  3. Pingback: Neat idea for showing Sitecore Sublayouts | Andy Burns' Blog

  4. Andy Burns says:

    This idea works pretty well, though I have had some fun with this on a page that uses an UpdatePanel, just as a warning. I’m not quite sure what the problem is, but if I remove the injected comments, it goes away!

  5. Jan Löwgren says:

    Reblogged this on Coresighted and commented:
    A very neat solution with a huge reward. Well done!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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