【问题标题】:Adding Scripts to ScriptManager on condition根据条件向 ScriptManager 添加脚本
【发布时间】:2010-12-10 02:30:44
【问题描述】:

我有一个棘手的场景,我想仅在某些条件下添加 ScriptManager 脚本引用,如下所示

<asp:ScriptManagerProxy ID="scriptManagerProxy1" runat="server">
<CompositeScript>
    <Scripts>
        <asp:ScriptReference path=/...." />
    </Scripts>
</CompositeScript>
<asp:ScriptManagerProxy>

我想让这个脚本只在特定条件下引用,所以我做了如下

<% if(xyzclass.property)
{ %>

above code

<% } %>

一旦我这样做了,我就会得到错误

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

我用谷歌搜索并尝试将“#”添加为

Expected class, delegate, enum, interface, or struct

我也尝试过这里提到的工作 http://leedumond.com/blog/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/

到目前为止没有运气。如果我采用上面链接中提到的方法,它会说类似

The base class includes the field '', but its type (System.Web.UI.ScriptManagerProxy) is not compatible with the type of control (System.Web.UI.ScriptManager).

伙计们,我需要的只是通过 ScriptManager 动态添加脚本。有什么方法在实践中也很好。

提前致谢,

尼米什

【问题讨论】:

    标签: c# asp.net asp.net-ajax


    【解决方案1】:

    如果你想根据条件添加脚本,可编程添加它们:

    ScriptManager mgr = ScriptManager.GetCurrent(this.Page);
    if (condition)
       mgr.Scripts.Add(new ScriptReference { Path = "~/script.js" });
    

    在后面的代码中。或者,使用 ScriptManagerProxy 并在用户控件或页面本身中定义它们。这是添加脚本的好方法,但如果您使用复合脚本,它会将这些添加到与 ScriptManager 相同的复合脚本中。

    HTH。

    【讨论】:

    • 嗨,伙计,已经这样做了,但也不起作用。当我执行 ScriptManger1.CompositeScript.Scripts.Add(item) 时,它不起作用。网页中未加载脚本。使用 Firebug 查看脚本是否在 ScriptReference 中。
    • 添加到页面上的特定 ScriptManagerProxy 控件对我有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 2020-06-09
    相关资源
    最近更新 更多