Question

I have this ScriptManager defined in my master page:

    <asp:ScriptManager runat="server">
        <Scripts>
            <asp:ScriptReference Name="jquery" />
            <asp:ScriptReference Name="bootstrap" />
            <asp:ScriptReference Name="respond" />
        </Scripts>
    </asp:ScriptManager>

I also have this ScriptResourceMapping defined in my BundleConfig.cs:

        ScriptManager.ScriptResourceMapping.AddDefinition("respond",
            new ScriptResourceDefinition {
                Path = "~/Scripts/respond.min.js",
                DebugPath = "~/Scripts/respond.js"
            });

        ScriptManager.ScriptResourceMapping.AddDefinition("jcrop",
            new ScriptResourceDefinition {
                Path = "~/Scripts/jquery.jcrop.min.js",
                DebugPath = "~/Scripts/jquery.jcrop.js"
            });

I can see that the "respond" mapping is loaded fine.

Now I need the "jcrop" mapping loaded only on a specific page so, il the page load event of that page I added:

        ScriptManager.GetCurrent(Page).Scripts.Add(new ScriptReference("jcrop"));
        ScriptManager.GetCurrent(Page).Scripts.Add(new ScriptReference("~/Scripts/picturecrop.js"));

I can see that the picturecrop.js script is loaded but the jcrop mapping is simply ignored. No error and no effects. If I change the mapping to the explicit path it works fine but I cannot leverage the Path/DebugPath distinction.

What am I missing to be able to programmatically use the ScriptResourceMapping I defined?

Thank you.

Was it helpful?

Solution

I did this two ways which both seemed to work:

ScriptManager.GetCurrent(Page).Scripts.Add(new ScriptReference("jcrop", null));

This one I think works because if you look at the two constructors to ScriptReference the one-parameter constructor has its parameter named path and in the two-param constructor they are name and assembly. So I think your problem is that it was expecting a full path and you were giving it just a name.

The other way I did it was:

ScriptManager.RegisterNamedClientScriptResource(Page, "jcrop");

Which also seemed to work just fine. I hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top