Tom Gullen
Tom Gullen

Reputation: 61729

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

This is a bit different to all the other questions as they all seem to refer to the head content specifically. With this one, I have a user control with the following placeholder:

<asp:PlaceHolder runat="server" ID="DiscussIncludes">    
    <script>
        var OnLastPage = <asp:Literal runat="server" ID="OnLastPageJS" />;
        var AJAXWait = false;
        var MinChars = <%=Settings.MinimumCommentChars%>;
        var AJAXURL = "<%=ResolveClientUrl("~/Handlers/DiscussAjaxHandler.ashx")%>";
        var CurrUsername = "<%=ThisUser.Username %>";
        var GravHash = "<asp:Literal runat="server" ID="GravJSRef" />";
        var RelURL = "<%=ResolveClientUrl("~/users/")%>";
        var Anch = "<%=Anchor.ToString()%>";
        var MyRep = "<%=MyRepString%>";
        var CurrReportID = 0;
        var LastPageURL = "<asp:Literal runat="server" ID="JSLastPageURL" />";
        var AllowedChars = <%=Settings.MaxCommentChars %>;
    </script>
    <script src="<%=CommonFunctions.AllocateStaticPath("/js/Discuss.js?v=" + Settings.JSVersionID)%>"></script>
    <script src="<%=CommonFunctions.AllocateStaticPath("/js/BlockUI.js?v=" + Settings.JSVersionID)%>"></script> 
</asp:PlaceHolder>

In the code behind I have:

ContentPlaceHolder FooterControl = (ContentPlaceHolder)Page.Master.FindControl("JavascriptIncludes");
FooterControl.Controls.Add(DiscussIncludes);

This throws the error:

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

On the FooterControl.Controls.Add(DiscussIncludes); line. I've tried changing all the <%= to <%# within the placeholder but no luck.

One point to note is this control works fine on all my other pages. Any ideas what would be causing this?

Upvotes: 1

Views: 881

Answers (2)

Amit Rai Sharma
Amit Rai Sharma

Reputation: 4225

I tried the following on my system and it seems to be working...

<asp:PlaceHolder runat="server" ID="DiscussIncludes">    
    <script type="text/javascript">
        var path = <%=Common.path%>;
    </script>
    <script type="text/javascript" src='<%=Common.GetScriptPath("jquery-1.4.1-vsdoc.js")%>'></script>
    <asp:PlaceHolder runat="server" ID="JsIncludes"> 
    </asp:PlaceHolder>
</asp:PlaceHolder>

on code behind..

    LiteralControl include = new LiteralControl(string.Format("<script src='{0}'></script>", Common.GetScriptPath("jquery-1.4.1.min.js")));
JsIncludes.Controls.Add(include);

I get the exception if i try to add the control to DiscussIncludes. However if add the control to JsIncludes it works. JsIncludes control is a child placeholder of DiscussIncludes.

Upvotes: 0

Wyatt Barnett
Wyatt Barnett

Reputation: 15673

Nothing in this control is causing the problem -- if it happens when you add the control to a particular environment and only that environment the that points at the issue. What is going on on the complaining page?

Upvotes: 0

Related Questions