2

就在渲染页面之前(或期间),我想附加一段代码(java脚本)。但是,当我尝试通过 Page.Controls 属性添加新的 LiteralControl 时,出现错误:

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

如何解决这个问题?

4

2 回答 2

3

为避免该问题,请执行以下操作:

  1. <asp:Placeholder id="Placeholder1" runat="server" />在 ASPX 中添加控件
  2. 在后面的代码中,将控件添加到该占位符的 Controls 集合中:

    Placeholder1.Controls.Add(myControl);

于 2010-05-18T13:15:59.017 回答
0

您可以将其添加LiteralControl到您的标记中并Text从代码隐藏中设置其属性。

编辑:

另一个(可能更正确)的选择是使用ScriptManager.RegisterClientScriptBlock

于 2010-05-12T12:57:43.900 回答