解决Ajax AsyncFileUpload引发的Controls集合无法修改错误
问题概述
遇到「The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)」错误,经排查确认是Ajax AsyncFileUpload控件导致——移除该控件后错误消失。其他页面运行正常,说明母版页本身无问题。
已尝试的无效方案
- 将代码块包裹在
<asp:PlaceHolder Runat="server">中 - 确保母版页无
<%= %>代码块 - 将所有JS文件引用包裹在PlaceHolder中
- 在母版页后台添加
Page.Header.DataBind()
母版页相关代码
<asp:PlaceHolder runat="server"> <script src='<%# ResolveUrl("path_to_script") %>' ... </asp:PlaceHolder> <link rel="stylesheet" href="~/assets/css/whatever.css" />
完整错误信息
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
引发问题的控件代码
<ajaxToolkit:AsyncFileUpload runat="server" ID="fuDBData" ClientIDMode="Static" class="hidden" ThrobberID="myThrobber" UploaderStyle="Traditional" OnUploadedComplete="fuDBData_UploadedComplete" OnUploadedFileError="UploadedFileError" onClientUploadStart="uploadStart" OnClientUploadComplete="uploadComplete" />
内容的提问来源于stack exchange,提问作者NoBullMan

