将脚本添加到ScriptManager的条件
问题描述:
我有一个棘手的情况,由此我想只有在某些条件下添加的ScriptManager的ScriptReference如下将脚本添加到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. <% ... %>).
我用Google搜索并尝试添加“#”作为<%#,但通过添加“#”无法找到类(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 ONLY动态添加脚本。有没有什么方法在实践中也是好的。
由于提前,
的NiMesh
答
如果你想添加基于条件的脚本,可编程添加它们:
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中是否存在脚本。 – DotNetInfo 2010-12-10 03:04:11