combine scripts in asp.net ajax toolkit

Herman picture Herman · Mar 9, 2009 · Viewed 8k times · Source

We are using version 3.0.20229.0 of the asp.net ajaxControlTookKit (before .net 3.5 sp1). I was wondering if I can combine our custom javascript files into the ScriptResource.axd that the controlTookKit creates. I've found this article (http://blogs.msdn.com/delay/archive/2007/06/11/script-combining-made-easy-overview-of-the-ajax-control-toolkit-s-toolkitscriptmanager.aspx) which tells me that I need to add the scriptCombine attribute to the assembly file. We are running a WebSite project, how can I add this attribute?

Answer

Zhaph - Ben Duguid picture Zhaph - Ben Duguid · Mar 9, 2009

You would need to add the scripts as resources to a seperate library and reference them from there to take advantage of the script combiner.

Edit to provide an walk through

Create a new Class Library project (called for example "CombinedScipts"), remove the default class.

Add a reference to both the AjaxControlToolkit and System.Web

Add your JS files to the project, and change their Build Action property to "Embedded Resource".

Open the AssemblyInfo.cs file

Add the following:

// You need to add a web resource call for each JS file in the project
[assembly: WebResource("CombinedScripts.Console.js", "text/javascript")]
[assembly: WebResource("CombinedScripts.Utilities.js", "text/javascript")]
// Not setting IncludeScripts or ExcludeScripts marks all scripts as
// combinable.
[assembly: AjaxControlToolkit.ScriptCombine()]

Add this library as a reference in your web site project.

In your project, you can then add the following between the ToolkitScriptManager tags:

<Scripts>
  <asp:ScriptReference name="CombinedScripts.Console.js" 
                       assembly="CombinedScripts" />
  <asp:ScriptReference name="CombinedScripts.Utilities.js"
                       assembly="CombinedScripts" />
</Scripts>

Not forgetting to ensure that the CombineScripts property of the ToolkitScriptManager is set to true.

This then results in one call to something like: /pageName.aspx?_TSM_HiddenField_=ToolkitScriptManager1_HiddenField&[...]

Which will have your combined scripts in, with comment delimiters like:

//START CombinedScripts.Console.js
[...]
//END CombinedScripts.Console.js
//START CombinedScripts.Utilities.js
[...]
//END CombinedScripts.Utilities.js