DotNetNuke Powered!
          
John Mitchell's (mostly DotNetNuke) Blog - The Controls collection cannot be modified because the control contains code blocks
 Saturday, November 19, 2005

This error happens when asp.net encounters code blocks like <%= %>, or <%# %>, in a User Control and the code-behind the User Control is trying to modify the controls collection of the page (usually a LoadControl statement).

In DotNetNuke, the modules are loaded into the the Skin Panes dynamically using LoadControl.

The Skin parser for DotNetNuke that makes the HTML Skin into an ASCX (UserControl) Skin on upload automatically places a variable called <%= SkinPath %> in your img src= attributes so that they will be portable and reference the image at runtime without having to know anything about where it is actually physically located when it is installed.

Now this is where you can get into trouble and find yourself getting the error if you are not careful about where you place your images in your skins.  

In DotNetNuke, an element such as <TD>, <P>, <SPAN>, or <DIV>, that is marked with the runat=server attribute will become a Skin Pane, and if modules are added to this Skin Pane and your Skin Pane has code blocks in it (<%= SkinPath %>), you are going to get the error "The Controls collection cannot be modified because the control contains code blocks ". 

So, to avoid this, make sure you don't have any images elements inside your Skin Panes.

This will not work:
<td id="ContentPane" runat="server"><img src="image.gif"></td>

However, if you do something like this it will work:
<td > <img src="image.gif"><span id="ContentPane" runat="server"></span></td>

Also, the example above used an image, but DNN will add the <%=SkinPath%> to other file references as well.

11/19/2005 10:59:17 AM (Central Standard Time, UTC-06:00)  #    Comments [7]