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:
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) { InjectControlPaths(Controls); } } protected void InjectControlPaths(ControlCollection controlCollection) { foreach (Control control in controlCollection) { if (control is UserControl) { try { 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); } } InjectControlPaths(control.Controls); } }
Notice how the control tree is only rendered when in debugging mode. A very simple and clever solution to a common nuisance. Thanks guys.
