开发者

Adding Scripts to ScriptManager on condition

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜