"The Controls collection cannot be modified because the control contains code blocks" "The Controls collection cannot be modified because the control contains code blocks" asp.net asp.net

"The Controls collection cannot be modified because the control contains code blocks"


First, start the code block with <%# instead of <%= :

<head id="head1" runat="server">  <title>My Page</title>  <link href="css/common.css" rel="stylesheet" type="text/css" />  <script type="text/javascript" src="<%# ResolveUrl("~/javascript/leesUtils.js") %>"></script></head>

This changes the code block from a Response.Write code block to a databinding expression.
Since <%# ... %> databinding expressions aren't code blocks, the CLR won't complain. Then in the code for the master page, you'd add the following:

protected void Page_Load(object sender, EventArgs e){  Page.Header.DataBind();    }


I just ran into this problem as well but found another solution.

I found that wrapping the code blocks with a asp:PlaceHolder-tag solves the problem.

<asp:PlaceHolder runat="server">  <meta name="ROBOTS" content="<%= this.ViewData["RobotsMeta"] %>" /></asp:PlaceHolder>

(The CMS I'm using is inserting into the head-section from some code behind which restricted me from adding custom control blocks with various information like meta-tags etc so this is the only way it works for me.)


I can confirm that moving the javascript with <% %> tags from the head to the form tag fixes this error

http://italez.wordpress.com/2010/06/22/ajaxcontroltoolkit-calendarextender-e-strana-eccezione/