Home → Resources → Articles

The Controls Collection Cannot Be Modified Because The Control Contains Code Blocks

(Microsoft .NET,Microsoft ASP.NET) by Jason Skowronek on 04/07/2011

Over the years, I have run into this issue multiple times and have decided to document my top two solutions. There are plenty more out there, these are mine.

Problem

You attempt to run ASP.NET code blocks (<%= %>), such as trying to resolve the application root using

<script href="<%= ResolveUrl("~/Js/Default.js") %>" />

and have the <head /> tag of your Master Page or Web Form set to runat="server" you will get the following runtime error:

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

Solutions

Here are (in my opinions) the cleanest solutions I have found with the least impact on code behind and time to implement.

Solution One

Wrap your code blocks in a server control. I use a plain-ole PlaceHolder control.

<asp:PlaceHolder runat="server">
<script src="<%= ResolveUrl("~/Js/jquery.js") %>" type="text/javascript"></script>
</asp:PlaceHolder>

This prevents the error but also does not render an actual control. All good.

Solution Two

Use a databinding tag <%# %> 

<script src="<%# ResolveUrl("~/Js/jquery.js") %>" type="text/javascript"></script>

The only caveat to this is that you need to fire the DataBind event for the head control onLoad or onInit.

Page.Header.DataBind();

Of the two solutions, I find option one my preferred as it requires less (no) back-end code.

Related Articles

Comments (5)

Keyur

8/5/2011 5:01:22 AM
The "solution One" worked like a charm! You saved my many hours... Thank you

keyur

8/5/2011 5:02:22 AM
The solution worked like a charm You have saved many hours of mine..! Thank you

Ronald

8/5/2011 9:28:17 PM
awesome post & solution. This just resolved my headache and i was wondering where i am going wrong...!!! Thanks again.

Ronald

8/5/2011 9:29:03 PM
I forgot to mention, Solution 1 did the magic. :o)

Ashutosh Bajpai

4/16/2012 12:41:47 PM
Hi, Really useful solution. Thanks a lot. Ashutosh Bajpai
Leave a comment
Name *
Email *
Homepage
Comment

Recommend

System Mechanic 

 

SkoNet provides comprehensive digital consulting services such as: web development, applications development, database design and architecture, business process management, customer relationship management, and many others that help businesses of every size, industry, and geography meet the complex challenge of managing and sharing information on the web. Our skills and expertise in online systems allow us to help customers build applications ranging from simple, single-page web sites to robust enterprise systems.

Online Backup, Ektron Consulting, Ektron Programmer, Ektron Developer, Ektron Partner Utah, Ektron Partner, Ektron Architect, Ektron Hosting, Salesforce.com Consultant Utah, Salesforce.com Partner Utah, Salesforce.com Partner, Salesforce.com Programmer, Salesforce.com Architect, Salesforce.com APEX Programmer