How to combine own scripts using the ToolkitScriptManager?

Last post 04-17-2008 3:16 PM by David Anson. 5 replies.

Sort Posts:

  • How to combine own scripts using the ToolkitScriptManager?

    04-11-2008, 3:12 AM
    • Member
      point Member
    • sgingter
    • Member since 03-05-2008, 9:26 AM
    • Germany
    • Posts 5

    Hi,

    I tried to combine my own scripts using the ToolkitScriptManager, but that does not work.
    I still get a request to scriptresource.axd for every of my own javascript files, but all requests to the ASP.NET AJAX are combined.

    I'll tell you what I did:

    I put a javascript into the project and switched the build action to 'Embedded Resource'.

    I added these lines of code to the assembly:

    [assembly: ScriptCombine()]
    [assembly: WebResource("My.Name.Space.swfobject.js", "application/x-javascript")]
    
     
    I registered the Script with this line (in prerender - is this too late?):
    ToolkitScriptManager.RegisterClientScriptResource(this.Page, typeof(SwfObject), "My.Name.Space.swfobject.js");


    But every script I add this way is reqeuested the old fashioned way through scriptresource.axd and not via the combined scripts request. 
    What did I wrong? 

     
    Thanks,

       Sebastian

     

  • Re: How to combine own scripts using the ToolkitScriptManager?

    04-15-2008, 2:44 PM
    • Star
      8,710 point Star
    • David Anson
    • Member since 04-10-2006, 9:39 PM
    • Microsoft
    • Posts 1,842
    • AspNetTeam

    Three quick suggestions:

    1. Double-check that the fully qualified script name you're passing to WebResource is the actual name of the script in the DLL. Reflector can help with this and it's common for the actual path to be different than expected - usually because the build process inserts an additional namespace.

    2. Add ClientScriptResourceAttribute to your Extender instead of calling RegisterClientScriptResource directly. This is what all the Toolkit controls do.

    3. Set a breakpoint on ToolkitScriptManager.OnResolveScriptReference and trace exactly what's happening when your script is encountered.

    Hope this helps!


    http://blogs.msdn.com/delay

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: How to combine own scripts using the ToolkitScriptManager?

    04-16-2008, 2:58 AM
    • Member
      point Member
    • sgingter
    • Member since 03-05-2008, 9:26 AM
    • Germany
    • Posts 5

    Hi,

    1.) Checked. That matches

    2.) I do not have an Extender. I simply want to include the SwfObject script or add a normal Javascript file I need in the context of several controls with the help of the WebResource mechanism. Except of the script combining that works like a charm.

    3.) That method does not get called for my scripts :-(

     

  • Re: How to combine own scripts using the ToolkitScriptManager?

    04-16-2008, 4:27 PM
    • Star
      8,710 point Star
    • David Anson
    • Member since 04-10-2006, 9:39 PM
    • Microsoft
    • Posts 1,842
    • AspNetTeam

    I've had a look and it appears that ScriptManager.RegisterClientScriptResource doesn't add things to the same list that is used by extenders with ClientScriptResourceAttribute and that's why it's not working as you'd like. My suggestion would be to create a trivial extender whose only purpose is to include the script you want. Then you can just add an instance of that extender to your page and the script will automatically be combined as we've discussed.

    Here's a trivial implementation of one such extender:

    using System.Web.UI;
    using System.Web.UI.WebControls;
    using AjaxControlToolkit;
    
    [assembly: WebResource("ClassLibrary1.JScript1.js", "text/javascript")]
    [assembly: ScriptCombine]
    
    namespace ClassLibrary1
    {
        [TargetControlType(typeof(Control))]
        [ClientScriptResource("ClassLibrary1.Class1", "ClassLibrary1.JScript1.js")]
        public class Class1 : ExtenderControlBase
        {
        }
    }
    

    http://blogs.msdn.com/delay

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: How to combine own scripts using the ToolkitScriptManager?

    04-17-2008, 5:20 AM
    • Member
      point Member
    • sgingter
    • Member since 03-05-2008, 9:26 AM
    • Germany
    • Posts 5

    Is there any way to enforce including a script without the need to use either a Extender control or a Script control? In other words: Without the need to create an additional control instance for every script include?

    I cannot derive my controls from ScriptControlBase to use the RequiredScript attribute directly on them because I rely on the CompositeControl base class.

    A also don't want to create a dummy extender control instance as a child control of every control thats relies on those scripts.

    Instead of calling a single liner (RegisterClientScriptResource) for each required script as I do now, I would need to create a extender object for each script, put them in the child control collection and I fear I have to render them too. Thats frankly 5 lines for each script (including assigning TargetControlID), on every single script requiring control, requires me to additionally override some methods on several controls and is not a cool solution. Thats a LOT of unnecessary overhead for just including a javascript code file only when it's needed. :-/

    A 'cheaper' method would be ripping the scripts out of the assemblies again, combine them in one big file and include that on every dynamic page, regardless if the scripts are needed or not depending on what controls are currently on that page.
    But that's pretty uncool too.

    Isn't there a 'cool' solution to include a simple combineable script file?
     

  • Re: How to combine own scripts using the ToolkitScriptManager?

    04-17-2008, 3:16 PM
    • Star
      8,710 point Star
    • David Anson
    • Member since 04-10-2006, 9:39 PM
    • Microsoft
    • Posts 1,842
    • AspNetTeam

    I wanted to make sure I wasn't missing anything obvious, so I checked with some of the ASP.NET AJAX experts. Mike Harder came back quickly with a great suggestion - I tried it and it works. Instead of the RegisterClientScriptResource line you were using unsuccessfully, I find that the following line worked perfectly in my sample project:

    ToolkitScriptManager.GetCurrent(Page).Scripts.Add(new ScriptReference("ClassLibrary1.JScript2.js", "ClassLibrary1"));

    http://blogs.msdn.com/delay

    This posting is provided "AS IS" with no warranties, and confers no rights.
Page 1 of 1 (6 items)