根据条件将脚本添加到ScriptManager [英] Adding Scripts to ScriptManager on condition

查看:70
本文介绍了根据条件将脚本添加到ScriptManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个棘手的情况,我只想在以下某些条件下添加ScriptManager脚本引用

I have a tricky scenario whereby I want to add ScriptManager scriptreference only on some conditions as follows

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

我只想在特定条件下引用此脚本,所以我做了如下操作

I want to make this script reference only on specific condition, so I did as below

<% if(xyzclass.property)
{ %>

above code

<% } %>

一旦我这样做,我会得到

Once I do that, I get the error as

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

我用谷歌搜索并尝试将'#'添加为<%#,但是通过添加'#'却找不到类(xyzclass),因此得到的错误是

I googled up and tried to add '#' as <%# but by adding '#' it can't find the class (xyzclass) and so get the error as

Expected class, delegate, enum, interface, or struct

我也尝试做这里提到的工作 http://leedumond.com/blog/the-controls-collection-不能被修改,因为该control-contains-code-blocks/

I tried doing the work as mentioned here too http://leedumond.com/blog/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/

到目前为止没有运气.如果我采用上述链接中提到的方法,则表示类似

No luck so far. If I take the approach as mentioned in above link, it says something like

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动态添加脚本.在实践中也有什么方法是好的.

Guys, what I need is just to add scripts via ScriptManager ONLY dynamically. Is there any way which is in practice good one too.

预先感谢

尼姆什

推荐答案

如果要根据条件添加脚本,请以可编程方式添加它们:

If you want to add scripts based on conditions, programmably add them:

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

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

in code behind. Or, use a ScriptManagerProxy and define them in the user control or page itself. This is a great way to add scripts, but if you use a composite script, it does append these to the same composite script as the ScriptManager.

HTH.

这篇关于根据条件将脚本添加到ScriptManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆