Reputation: 1539
ASP.NET 3.5 SP1 adds a great new ScriptCombining feature to the ScriptManager object as demonstrated on this video. However he only demonstrates how to use the feature with the ScriptManager on the same page. I'd like to use this feature on a site where the scriptmanager is on the master page but can't figure out how to add the scripts I need for each page programmatically to the manager. I've found this post to use as a starting point, but I'm not really getting very far. can anyone give me a helping hand?
Thanks, Dan
Upvotes: 1
Views: 2129
Reputation: 3665
You can also do this in markup using ScriptManagerProxy.
You can add the ScriptManager to the master page e.g.
<asp:ScriptManager ID="ScriptManager" runat="server">
<CompositeScript>
<Scripts>
<asp:ScriptReference name="WebForms.js" assembly="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<asp:ScriptReference name="MicrosoftAjax.js" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<asp:ScriptReference name="MicrosoftAjaxWebForms.js" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</Scripts>
</CompositeScript>
</asp:ScriptManager>
And then add the ScriptManagerProxy to the content page e.g.
<asp:Content ID="HomeContent" ContentPlaceHolderID="PlaceHolder" runat="Server">
<asp:ScriptManagerProxy runat="server">
<CompositeScript>
<Scripts>
<asp:ScriptReference Path="~/yourscript.js" />
</Scripts>
</CompositeScript>
</asp:ScriptManagerProxy>
Upvotes: 1
Reputation: 3908
Give this a shot:
ScriptReference SRef = new ScriptReference();
SRef.Path = "~/Scripts/Script.js";
ScriptManager.GetCurrent(Page).CompositeScript.Scripts.Add(SRef);
That will get the current scriptmanager (even if it is on a master page) and add a script reference to the CompositeScript properties.
Upvotes: 2