"The Controls collection cannot be modified" error

28 Views Asked by At

I have been struggling with this error for more than half day and none of the suggestions helps, including SOF's Getting error about control collection .... I am adding this because I am pretty sure someone is going to mark this as "duplicate"; it is not.

I know Ajax AsyncFileUpload is the culprit but I can's see why. When I remove this control from the page, the error goes away.

I have also tried all suggestion about wrapping code blocks inside placeholder tag, to no avail. Also, all other pages work fine which means it cannot be an issue with master page otherwise all would have problems (I think!).

I tried:

  • Wrapping tags in placeholder <asp:PlaceHolder Runat="server">
  • Nothing in master page with '<%= %>'
  • Wrapped all references to js files in placeholder
  • Added Page.Header.DataBind() to master page codebehind.

In masterpage:

   <asp:PlaceHolder runat="server>
    <script src='<%# ResolveUrl("path_to_script:) %>'
    ...
   </asp:PlaceHolder>

   <link rel="stylesheet" href="~/assets/css/whatever.css" />

The page displays this error:

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).                                
[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).]
   System.Web.UI.ControlCollection.Add(Control child) +11686804
   AjaxControlToolkit.ToolkitResourceManager.RegisterCssReferences(Control control) +893
   System.Web.UI.Control.LoadRecursive() +90
   System.Web.UI.Control.LoadRecursive() +185
   System.Web.UI.Control.LoadRecursive() +185
   System.Web.UI.Control.LoadRecursive() +185
   System.Web.UI.Control.LoadRecursive() +185
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1602

The control causing this issue is:

<ajaxToolkit:AsyncFileUpload runat="server" ID="fuDBData"
    ClientIDMode="Static"
    class="hidden"
    ThrobberID="myThrobber"
    UploaderStyle="Traditional"
    OnUploadedComplete="fuDBData_UploadedComplete"
    OnUploadedFileError="UploadedFileError"
    onClientUploadStart="uploadStart"
    OnClientUploadComplete="uploadComplete" />
1

There are 1 best solutions below

0
NoBullMan On

While grasping at straws trying to sort this issue out, I made a change in site.master file that seems to have fixed the issue. Although, I still don't understands while other page with non-Ajax controls did not barf like this one with Ajax AsyncFileUpload did.

Nevertheless, this is the change that fixed my issues; if it helps one person not to have the migraine that I had, I;ll be happy!

This is what I had in site.master:

<head>
    ...
    <script type="text/javascript">
        //Access Code-Behind Data in Javascript
        var Role = "<%#User_Role%>";
        var UserName = "<%#User_Name%>";
        var validRoles = "<%#ValidRoles%>";
        ...
    </script>
</head>

This is what I changed it to:

<head>
    ...
</head>
<body>
    <script type="text/javascript">
        //Access Code-Behind Data in Javascript
        var Role = "<%#User_Role%>";
        var UserName = "<%#User_Name%>";
        var validRoles = "<%#ValidRoles%>";
        ...
    </script>
    <form id="Form1" runat="server">
        ...
    </form>
</body>