Press enter to see results or esc to cancel.

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

I recently ran across the following ASP.NET exception:

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

It was something I hadn’t encountered before and I couldn’t understand what was going on.

It would probably help if I divulged some of the factors that contributed to me coming across this exception, so here goes.

I was actually loading an ascx user control dynamically and I was doing it from an aspx Master page. The code blocks (<% %>) were inside the Master page. Better?

Anyways, the solution to this weird problem – one that should be fixed by the people who created visual studio (yes Microsoft, I am looking at you) – is to place the offending code within PlaceHolder tags.

<asp:PlaceHolder runat="server"><% some code here %></asp:PlaceHolder>

Simple, isn’t it? I actually found the solution from this StackOverflow post.

Comments

Leave a Comment