Sunday, January 25, 2009

HttpException: The Controls collection cannot be modified because the control contains code blocks

A team mate had a pretty odd error from ASP.NET that comes up like this...

Server Error in '/LaaziWeb' Application.

Stack Trace:

[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).]
System.Web.UI.ControlCollection.Add(Control child) +8674071
System.Web.UI.PageTheme.SetStyleSheet() +478
System.Web.UI.Page.OnInit(EventArgs e) +8694828
System.Web.UI.Control.InitRecursive(Control namingContainer) +333
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +378

The error was a little hard to put a finger on, but I eventually traced it to a script that used a <%= txtBox1.ClientID %> with a document.getElementById. The script was in the HEAD section of the page and to make the exception disappear, I moved the script into the BODY section.

No comments: