Dot*_*nfo 4 c# asp.net asp.net-ajax
我有一个棘手的场景,我想在某些条件下添加ScriptManager脚本引用,如下所示
<asp:ScriptManagerProxy ID="scriptManagerProxy1" runat="server">
<CompositeScript>
<Scripts>
<asp:ScriptReference path=/...." />
</Scripts>
</CompositeScript>
<asp:ScriptManagerProxy>
Run Code Online (Sandbox Code Playgroud)
我想仅在特定条件下制作此脚本参考,所以我做了如下
<% if(xyzclass.property)
{ %>
above code
<% } %>
Run Code Online (Sandbox Code Playgroud)
一旦我这样做,我得到错误
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Run Code Online (Sandbox Code Playgroud)
我google了,试图将'#'添加为<%#但是添加'#'它找不到类(xyzclass),所以得到错误为
Expected class, delegate, enum, interface, or struct
Run Code Online (Sandbox Code Playgroud)
我也尝试过这里提到的工作 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).
Run Code Online (Sandbox Code Playgroud)
伙计们,我需要的只是动态地通过ScriptManager添加脚本.有没有什么方法在实践中也很好.
提前致谢,
的NiMesh
如果要根据条件添加脚本,可以按可编程添加它们:
ScriptManager mgr = ScriptManager.GetCurrent(this.Page);
if (condition)
mgr.Scripts.Add(new ScriptReference { Path = "~/script.js" });
Run Code Online (Sandbox Code Playgroud)
在代码背后.或者,使用ScriptManagerProxy并在用户控件或页面本身中定义它们.这是添加脚本的好方法,但如果使用复合脚本,它会将这些脚本附加到与ScriptManager相同的复合脚本中.
HTH.
| 归档时间: |
|
| 查看次数: |
5756 次 |
| 最近记录: |