combine scripts in asp.net ajax toolkit

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x