无法修改控件集合,因为控件包含代码块(即 <% .. %>).Telerik.Web.UI.RadAjaxControl

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

本文关键字:控件 Telerik RadAjaxControl UI Web 集合 修改 因为 包含 代码      更新时间:2023-09-26
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:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).]
   System.Web.UI.ControlCollection.AddAt(Int32 index, Control child) +8668018
   Telerik.Web.UI.RadAjaxControl.MoveUpdatePanel(Control initiator, Control updated) +133
[HttpException (0x80004005): Please, see whether wrapping the code block, generating the exception, within RadCodeBlock resolves the error.]
   Telerik.Web.UI.RadAjaxControl.MoveUpdatePanel(Control initiator, Control updated) +181
   Telerik.Web.UI.RadAjaxControl.PerformRender() +375
   Telerik.Web.UI.RadAjaxControl.OnPageRender(HtmlTextWriter writer, Control page) +1222
   Telerik.Web.UI.RadAjaxControl.RenderPageInAjaxMode(HtmlTextWriter writer, Control page) +95
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

您也可以在不使用 telerik 的情况下遇到此问题,因此更好的解决方案是添加一个

palceholder 而不是 RadCodeBlock 适用于所有情况

    <asp:PlaceHolder ID="PlaceHolder1"
your script 
 runat="server"></asp:PlaceHolder>
如果您在

页面中使用了"<%= %> 或 <%# %>",则需要使用 RadcodeBlock .

例:

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
      //your script
   </telerik:RadCodeBlock>

就我而言,我在ASPX页面中几乎没有if-else块,如下所示。

<%if (showControl)
{%>
     <%--HTML Markup--%>
<%}%>

我把每个都包在一个RadCodeBlock里,错误就消失了。

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<%if (showControl)
{%>
    <%--HTML Markup--%>
<%} %>
</telerik:RadCodeBlock>

必须将每个这样的块包装在RadCodeBlock中。尝试包装整个 html 正文,但这不起作用。