> ## Content Index
> Fetch the complete content index at: https://blog.tsd.digital/llms.txt
> Use this file to discover other available public pages before exploring further.

# The Controls collection cannot be modified because the control contains code blocks
- URL: https://blog.tsd.digital/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/
- Published: 2007-08-24T08:49:56.000Z
- Updated: 2007-08-24T08:49:56.000Z
- Author: Tim Gaunt
- Tags: ASP.Net, C#, Web Development, .Net, #Import 2025-04-01 05:36

Server Error in '/' Application. *The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).* **Description:** An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. **Exception Details:** System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).**Source Error:**Line 132: metaKey.Name = "keywords";Line 133: metaKey.Content = p.MetaKeywords;Line 134: this.Page.Header.Controls.Add(metaKey);Line 135: }Line 136: if (!String.IsNullOrEmpty(p.MetaDescription))**Source File:** a:\\xyz\\ContentHandler.aspx.cs **Line:** 134 **Stack Trace:**\[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).\] System.Web.UI.ControlCollection.Add(Control child) +2105903 ContentHandler.Page\_Load(Object sender, EventArgs e) in a:\\xyz\\ContentHandler.aspx.cs:134 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +47 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061**Version Information:** Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832 

Another day, another issue ;)

This had me going around in circles for a while until I realised what it was, if you're getting this error you can bet your bottom dollar that you have <%= %> somewhere in your page's header -furthermore I'd hazard a guess that you've got it in some JavaScript to reference an ASP.Net control on the page- and then you're trying to add a control to the header programmatically (or a custom control from someone like [Telerik](http://www.telerik.com/?ref=blog.tsd.digital) is trying to). Am I right1?

1 I'm not allowed to ask you to so I won't, but if I was right, then spend that bottom dollar clicking on one of the Google Ads :P

I can't tell you exactly why this occurs but my understanding of it is that ASP.Net can't re-create the header if it has Response.Write somewhere in the header (<%=) -most likely due to when the header is created it's not available (will look into it). No doubt you want to know the fix?

**The Fix** The fix is simple, remove the inline code blocks and JavaScript and move it to your code behind i.e.:

string \_manageSearch = String.Format(@'...'"lblFindAGift"");string \_manageSearch = String.Format( @" function ManageSearch(){{ var lbl = document.getElementById(""lblFindAGift""); var txt = document.getElementById(""{0}""); var btn = document.getElementById(""{1}""); .Do Something with it.. }}", txtSearch.ClientID);this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ManageSearch", \_manageSearch, true);**Remember:** You need to escape the curly brackets otherwise you will get a "[String.Format- Exception of type System.Web.HttpUnhandledException was thrown](http://blogs.thesitedoctor.co.uk/tim/2007/08/22/StringFormat+Exception+Of+Type+SystemWebHttpUnhandledException+Was+Thrown.aspx?ref=blog.tsd.digital)"

**Update:** Thanks to Julian Voelcker for sending me [this alternative "fix" for the problem](http://west-wind.com/weblog/posts/5758.aspx?ref=blog.tsd.digital), can't say I like it though ;) basically instead of using <%= ... %> you would write the databinding expression of: <%**#** ... %>