Tim

Footprints in the snow of a warped mind

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

Where to find me

Flickr Icon  Twitter Icon  Linked In Icon  FaceBook Icon  Windows Live Alerts Butterfly  RSS 2.0 

Tag Cloud

AJAX (4) ASP (6) ASP.Net (52) Error Reporting (4) Web Service (2) WSDL (1) Atlas (2) Business (76) Business Start-up Advice (25) Client (14) Expanding Your Business (17) C# (17) Canoeing (4) Canoe Racing (5) Cheshire Ring Race (5) Racing (2) Training (4) CIMA (1) Cisco (1) 7970G (1) CSS (3) dasBlog (4) DDD (1) Design (9) Icons (1) Development (13) eCommerce (1) General (39) Christmas (6) Fun and Games (11) Internet (22) Random (46) RX-8 (8) Home Cinema (2) Hosting (2) IIS (10) iPhone (1) JavaScript (4) jQuery (1) Marketing (5) Email (1) Multipack (1) Networking (2) Nintendo (1) OS Commerce (1) Photography (1) PHP (1) PowerShell (2) Press Release (1) Productivity (2) Security (2) SEO (5) Server Maintenance (4) Server Management (9) Social Media (1) Social Networking (2) Experiment (1) Software (9) Office (5) Visual Studio (12) Windows (4) Vista (1) SQL (1) SQL Server (13) Stored Procedure (1) Testing (1) The Site Doctor (106) Turnover Challenge (1) Twitter (2) uCommerce (1) Umbraco (18) 2009 (1) Web Development (57) WebDD (33) Wii (1)

Blog Archive

Search

<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

Recent Comments

Blog Archive

Various Links

Blogs I Read

[Feed] Google Blog
Official Google Webmaster Central Blog
[Feed] Matt Cutts
Gadgets, Google, and SEO
[Feed] Ol' Deano's Blog
My mate Dean's blog on my space, equally as random as mine but not off on as much of a tangent!
[Feed] Sam's Blog
Sam is one of my younger brothers studying Product Design and Manufacture at Loughborough, this is his blog :) Enjoy!

Recent Tracks

last.fm - The Social Music Revolution

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

newtelligence dasBlog 2.2.8279.16125

Send mail to the author(s) Email Me (Tim Gaunt)

© 2010 Tim Gaunt.

Sign In

    # Friday, August 24, 2007

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

    Friday, August 24, 2007 10:49:56 AM (GMT Daylight Time, UTC+01:00)

    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 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( @" 
            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"

    Update: Thanks to Julian Voelcker for sending me this alternative "fix" for the problem, can't say I like it though ;) basically instead of using <%= ... %> you would write the databinding expression of: <%# ... %>

    Wednesday, February 20, 2008 7:31:07 AM (GMT Standard Time, UTC+00:00)
    Thanx a lotttt buddy...
    This trick works :)

    Happy Programming....
    Monday, April 07, 2008 11:22:48 PM (GMT Daylight Time, UTC+01:00)
    Also, if the js code block is in the <head>, make sure that you do not have runat="Server"...this was the cause of our problem. If,for some reason, you need to have the head generated on the server side, then take the JS code block outside of the head tag.
    Bad Koder
    Sunday, April 20, 2008 4:09:26 AM (GMT Daylight Time, UTC+01:00)
    Thanks a lot, helped me out...!
    Monday, May 19, 2008 5:07:37 PM (GMT Daylight Time, UTC+01:00)
    I am getting this error when trying to use the following code in the head section of a masterpage:
    <!--[if lte IE 7]>
    <style type="text/css"> @import url('<%= ResolveUrl("~/CSS/fieldset-styling-ie.css") %>;') </style>
    <![endif]-->

    OutOfTouch
    Friday, September 12, 2008 2:38:09 PM (GMT Daylight Time, UTC+01:00)
    I dont know if anyone else found this, but if you specify your script in the following manner, the problem will go away. Make sure you include the begin and end HTML commented out lines.

    <script type='text/javascript'>
    <!--
    (javacript goes here)
    //-->
    </script>
    Larry Pauley
    Saturday, January 17, 2009 2:17:00 PM (GMT Standard Time, UTC+00:00)
    to solve this problem just remove the skin from this page
    <pages theme=""

    sample web.config
    <location path="Forms">
    <system.web>
    <pages theme="" />
    </system.web>
    </location>
    Saturday, January 17, 2009 7:09:41 PM (GMT Standard Time, UTC+00:00)
    Thanks Tarek however there are no themes on the page. This error relates to using the string replacement placeholders (the {}'s).

    Tim
    Sunday, June 21, 2009 11:50:45 AM (GMT Daylight Time, UTC+01:00)
    Good Client Side Validation In Java Script
    Sanjay Gupta
    Friday, July 17, 2009 7:25:23 AM (GMT Daylight Time, UTC+01:00)
    If you came here with the same problem that "outoftouch" has, then here's the solution:
    (You don't need the resolveUrl method if using runat="server" on the head tag)
    http://www.dailycoding.com/Posts/the_script_tag_runatserver_problem_solution_using_resolveurl.aspx
    Tuesday, July 06, 2010 4:34:19 PM (GMT Daylight Time, UTC+01:00)
    Hi,
    thankyou very very match!! this post solved the problem i was on all the day!! thanks
    simveloper
    Name
    E-mail
    (will show your gravatar icon)
    Home page

    Comment (HTML not allowed)  

    Live Comment Preview