The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %> ).
I am pretty sure that any web developer would hate an exception that is hard to catch. One of those is the one which says “The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).”
In my case I got this exception on a page that was working perfectly about two days ago and now had been untouched since then. My first line of Googling defense resulted in articles like this article and other discussions that tell you to change the <%= xxxxx %> to <%# xxxxx %>. Below I show how my code was when I was getting the error.
However just changing the <%= sign to <%# or moving the script tag within the content placeholder did not make any difference because even though it removed the original issue but caused another problem. The JavaScript wouldn’t load. :S
Here is my solution that resolves the issue and still lets the JavaScript load.
Just inject the script tag in a literal in the head of the master page in the code behind.