Tag: Menu

The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>).

I am developing a data entry application in Visual Studio 11 Beta, by using the ASP.NET Dynamic Data Entities Web Application project template. I decided to add the ASP.NET Menu control to the Site.master master page, but then I started getting this error, The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>).

While the error more or less tells you where things go wrong, the stack trace looks like this:

[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>).]
   System.Web.UI.ControlCollection.Add(Control child) +9719987
   System.Web.UI.WebControls.MenuRendererStandards.AddStyleBlock() +61
   System.Web.UI.WebControls.MenuRendererStandards.PreRender(Boolean registerScript) +89
   System.Web.UI.WebControls.Menu.OnPreRender(EventArgs e) +238
   System.Web.UI.Control.PreRenderRecursiveInternal() +83
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974

Okay, so the rendering isn’t quite working. I did a quick search for this error and found that a lot of developers have had this issue over the years, so definitely nothing to do with the .NET Framework 4.5 or VS11 Beta. What fixed the issue was to move the script element, <script src="<%: ResolveUrl("~/Scripts/modernizr-2.0.6-development-only.js") %>"></script> out of the head element and into the body element. Very simple fix…

Advertisements

Hide Pages from ASP.NET Menu control

When you use the Menu control with the SiteMapDataSource control and an associated XML sitemap file, and also use the SiteMapPath control for creating the bread crumb, you often need to include specific pages in the sitemap file, but hide the same pages from the menu. If you don’t include a page in the sitemap file, it isn’t shown in the menu, which is what you want, but when you then browse to that specific page, your breadcrumb disappears. The option is to include the page in the sitemap file, but add a custom attribute, like this, where we want to hide the SearchResults.aspx page from the menu. Notice that we’ve added the HideFromMenu attribute with a value of true.

xml version="1.0" encoding="utf-8" ?>

        ...
        true"
             url="~/SearchResults.aspx" />

Continue reading “Hide Pages from ASP.NET Menu control”