views:

47

answers:

3

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?

A: 

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
My other pages do this and have no problems though.
Earlz
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
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.
Cory Larson
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
A: 

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
A: 

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

geekydevjoe
I do not ` ` ` `
Earlz