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