Adding Scripts to ScriptManager on condition

百般思念 提交于 2019-12-10 17:46:03

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/4405148/adding-scripts-to-scriptmanager-on-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!