无法修改 Controls 集合,因为该控件包含代码块(即 <% ... %>)

发布于 2024-11-25 07:29:49 字数 790 浏览 3 评论 0原文

我遇到了这个错误,并找到了解决方法,并且解决方案对我有用,但我想知道这是解决问题的最佳方法,并且我想确保它不会严重影响任何其他页面。希望专家给予帮助。 如果这是最好的解决方案,那么你们中的许多人都可以节省开支。

当代码块放置在母版页中时,会发生此错误。将代码块放在占位符中即可解决该问题。将 AJAX 扩展程序添加到网页时,它将尝试在头部注册脚本。如果母版页中存在代码块,则可能会发生错误。

要解决此问题,只需将代码块移动到母版页头部的占位符中,如下所示:

<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder ID="myPlaceholder" runat="server">
    <script language="javascript" type="text/javascript" src="<%= Page.ResolveClientURL("~/javascript/global.js")%>"></script>
    </asp:ContentPlaceHolder>
    <asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>

I am stuck with this error and found a work around and the solutions works for me, but i i would like to know is it the best way to fix the issue and i want to make sure that it wont affect any other pages badly. Hope the experts will help.
If this was the best solution then many of you can save your heads.

This error occurs when a code block is placed in the MasterPage. Place the code block in a placeholder to resolve the issue.When adding AJAX extenders to your Web page, it will attempt to register scripts in the head. If code blocks are present in the MasterPage, an error might occur.

To resolve this issue, simply move the code block into a placeholder in the head of your MasterPage, like so:

<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder ID="myPlaceholder" runat="server">
    <script language="javascript" type="text/javascript" src="<%= Page.ResolveClientURL("~/javascript/global.js")%>"></script>
    </asp:ContentPlaceHolder>
    <asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无戏配角 2024-12-02 07:29:49

该错误是逻辑错误,在使用 <%= %> 渲染后,您无法混淆渲染的控件

解决此问题的一种方法是使用文字控件,并在上渲染脚本行代码后面。

<asp:ContentPlaceHolder ID="myPlaceholder" runat="server">
    <asp:Literal runat="server" ID="txtIncludeScript" EnableViewState="false"></asp:Literal>
</asp:ContentPlaceHolder>

以及后面的代码。检查是否为 null,因为如果更改占位符,文字将为 null。还要设置 EnableViewState=false,因为您在每个 Page_Load 上设置它并且不希望将其保存到视图状态。

  if(txtIncludeScript != null)
  {
    txtIncludeScript.Text = 
string.Format("<script language=\"javascript\" type=\"text/javascript\" src=\"{0}\"></script>",   
Page.ResolveClientUrl("~/javascript/global.js"));
  }

The error is logical you can not confuse the rendered controls after they rendered by using the <%= %>

One way to solve this issue is to use a literal control, and render the script line on code behind.

<asp:ContentPlaceHolder ID="myPlaceholder" runat="server">
    <asp:Literal runat="server" ID="txtIncludeScript" EnableViewState="false"></asp:Literal>
</asp:ContentPlaceHolder>

and on code behind. Check for null because if you change the placeholder the literal is null. Also set EnableViewState=false because you set it on every Page_Load and you do not wish to save it to viewstate.

  if(txtIncludeScript != null)
  {
    txtIncludeScript.Text = 
string.Format("<script language=\"javascript\" type=\"text/javascript\" src=\"{0}\"></script>",   
Page.ResolveClientUrl("~/javascript/global.js"));
  }
清风疏影 2024-12-02 07:29:49

ContentPlaceHolder 需要一个母版页,因此您可以将该标记替换为可以在服务器上运行的其他元素,以防您的页面没有母版页并且您无法摆脱

<head id="Head1" runat="server">
 <title>Untitled Page</title>
 <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
 <div runat="server">
  <script language="javascript" type="text/javascript" src="<%= Page.ResolveClientURL("~/javascript/global.js")%>"></script>
 </div>
 <asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>

ContentPlaceHolder requires a master page, so you can replace that tag with some other element that can be ran at the server in case your page does not have a master page and you cannot get rid of the

<head id="Head1" runat="server">
 <title>Untitled Page</title>
 <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
 <div runat="server">
  <script language="javascript" type="text/javascript" src="<%= Page.ResolveClientURL("~/javascript/global.js")%>"></script>
 </div>
 <asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文