在条件下将脚本添加到ScriptManager(Adding Scripts to ScriptManager on condition)

编程入门 行业动态 更新时间:2024-10-25 23:28:38
条件下将脚本添加到ScriptManager(Adding Scripts to ScriptManager on condition)

我有一个棘手的场景,我想在某些条件下添加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. <% ... %>).

我用谷歌搜索并尝试添加'#'作为<%#但是添加'#'它找不到类(xyzclass),所以得到错误为

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添加脚本。 有没有什么方法在实践中也很好。

提前致谢,

的NiMesh

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. <% ... %>).

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

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).

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

Thanks in advance,

Nimesh

最满意答案

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

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

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

HTH。

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" });

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.

更多推荐

本文发布于:2023-08-04 16:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417500.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件下   脚本   ScriptManager   condition   Scripts

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!