1

I recently put some code <% %> code blocks in my Master Page. Note I've read of the "fix" for either moving things out of <head> or using <%# %> but neither of them work well for my application.

Now the weird thing is that I only get this error on one page of mine. All the other pages seem to work fine, so what actually causes this error? There is nothing I can think of that is unique about this page. It uses the script manager as does other working pages and there is just nothing extraordinary about this page. It does have quite a few custom controls on it, so hunting down what is different in this page is more difficult than usual.

So what actually causes the Controls collection cannot be modified because the control contains code blocks exception?

Earlz
  • 62,085
  • 98
  • 303
  • 499
  • Do you custom controls contain stuff inside `<% %>`, and if so, do you dynamically add your custom controls to the page? You mentioned the moving code out of `` as well; where exactly are your problematic code blocks? – Cᴏʀʏ Aug 11 '10 at 21:13
  • I do not know. I added a codeblock(literally as simple as `<% /*codesss*/ %>`) to the master page and now this page doesn't work. @Cory – Earlz Aug 11 '10 at 21:24
  • what are you trying to do inside the `<% %>` codeblock - that way we can help with a better approach – Pete Amundson Aug 11 '10 at 21:34
  • @Pete I literally have nothing more than a comment there right now. but if I remove the codeblock then it works. – Earlz Aug 11 '10 at 21:51
  • true...what i'm getting at is you won't be able to fix this exception without either a) removing the `<%...%>` or b) finding the control that exists somewhere in your control collection that is being dynamically added (or removed). But, if we knew what you wanted to do with the code block, we could offer other suggestions for you :) – Pete Amundson Aug 11 '10 at 22:02
  • I have upticked a comment because I have just created this problem for myself by specifying a Theme on the page ... is this 1 page you have probs with got a Them specified on it? – Aidanapword Aug 29 '12 at 14:43

4 Answers4

2

Things can go wrong when some code tries to add controls to the tag containing the <% ... %> or <%= ... %> code block (in this case your <head> tag).

For instance, when you're using themes, the Page class will automatically add <link> tags to the <head> for every CSS file in your theme's directory. But it could also be triggered by setting the Page.Title.

But there are many more ways that can cause modifications to the <head> tag, so without further information (such as a stacktrace) it's hard to give a definitive answer.

Ruben
  • 15,217
  • 2
  • 35
  • 45
2

I've come up with something I find a lot easier and more straightforward -- while leaving the tag in the header where it belongs.

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();    
   }

The DataBind method evaluates all the databinding expression(s) in your header at load time.

S Patel
  • 31
  • 1
1

If you have a page or control with <% %> and ever dynamically update the control collection (add a control to the page that isn't defined in the .aspx/.ascx) this error will trigger. To get around this I have used an <ASP:Literal/> to inject data instead of <% %>

Pete Amundson
  • 890
  • 5
  • 16
  • My other pages do this and have no problems though. – Earlz Aug 11 '10 at 21:51
  • If your other pages don't have dynamically added controls, then they won't have issues - also, you might not have any control over the dynamic controls (depending on UI frameworks/helpers you are using) – Pete Amundson Aug 11 '10 at 22:04
  • You could also use a `PlaceHolder` control, or any number of others, for that matter. If it's `UserControl`s causing a problem, I usually end up rendering them server-side and injecting their HTML into the page, seeing as most of what I do is AJAX-enabled interactivity and whatnot. – Cᴏʀʏ Aug 12 '10 at 02:56
  • I'm using regular ASP.Net WebForms and I mean I have stuff with `Controls.Add(...)` in other pages that do work. I'm trying to figure out why those pages work and my other page doesn't – Earlz Aug 12 '10 at 05:50
1

If you have themes enabled it could cause it to do that.

geekydevjoe
  • 108
  • 1
  • 7