5

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 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Mate, Already did that but doesn't work too. When I did ScriptManger1.CompositeScript.Scripts.Add(item), doesn't work. Script is not loaded in the webpage. Used Firebug to see if the scripts are there in ScriptReference.
Adding to particular ScriptManagerProxy control on the page worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.