Digiguru.co.uk

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

01 Apr 2011

Reading time: 1 minute

Had a really frustrating error in the web project where the following error message appeared... The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). I looked through my application and saw that I had the following code....
<head id="Head1" runat="server">
    <script language="javascript" type="text/javascript" src="/js/MasterJS.aspx?u=<%=ConfigurationManager.AppSettings("UpdateNumber")%>"></script>
    <asp:ContentPlaceHolder ID="ScriptExtra" runat="server"></asp:ContentPlaceHolder>
</head>
but I also inject into the header Meta information and title pages from my application using a nice little function to keep our Meta information in check. As a result - I needed to change all references from
<%#ConfigurationManager.AppSettings("UpdateNumber")%>
to
<%=ConfigurationManager.AppSettings("UpdateNumber")%>
As I result I needed to change all my headers that are runat="server" to use I needed to change all references that are found in the head from an equals to a hash. I used the following regular expression to find all references of a head with a runat server... [\<]head[^\>]*runat="server"[^\>]*> 152 files.