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]
6/7/2006 7:29:37 AM (Central Standard Time, UTC-06:00)

Hello John, i have a question, do you have an e mail to ask it?
6/7/2006 8:02:39 AM (Central Standard Time, UTC-06:00)
Go ahead and ask here.
I replied to your address, but got a bounce.
6/13/2006 6:13:40 AM (Central Standard Time, UTC-06:00)
hello John, i want to download captcha tool, please help where can i find it?
9/13/2006 6:35:03 PM (Central Standard Time, UTC-06:00)
Banged our heads for a while till we found yur post. Thank you for posting this.
10/10/2006 8:23:30 AM (Central Standard Time, UTC-06:00)
Hi ,
I solved this by using server image control with .ImageUrl property
myimg.ImageUrl="/img/" & CStr(Session("Language")) & "/logo.jpg"
1/5/2007 5:30:07 AM (Central Standard Time, UTC-06:00)
i want to download captcha tool
3/20/2007 7:35:15 PM (Central Standard Time, UTC-06:00)
OK, I found an img tag that I had added to try to create a particular look and I had the runat server for the content pane in the same TD. I tried your fix and it worked. I just wish I had found your blog sooner. I need to RSS it since it looks like you address some good topics.
Thanks.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):