Ajax Control Toolkit and ASP.NET Page Output Caching

My blog is moving to our 1stQuad Website and this blog-post has been republished here.

Using the ASP.NET page output caching can lead to problems with the AJAX Control Toolkit. You might see error messages such as

Javascript error: AjaxToolkit is undefined

While searching the web, i have found many people with the same problem, however no solution. Many even pointed to a post on Microsoft Connect that concludes with the following statement: “We do not support output caching in combination with ScriptControls, Extenders, or ScriptManagerProxies that need ScriptReferences at this time.”

However, there IS a way to make it actually working. Loren Halvorson finally brought me on the right path. While the post is mainly about how to speed up the loading time of AJAX Control Toolkit controls it includes an IHttp-Handler that will allow to use the CombineScriptsHandlerUrl property of the ToolkitScriptManager. I will try to explain the background and how I’ve implemented it within a SharePoint environment.

When working with the AJAX Control Toolkit it is advisable to use the ToolkitScriptManager control instead of the standard ASP.NET ScriptManager because it allows (amongst other advantages) script.combining. For more details I recommend the this reading. The ToolkitScriptManager loads all necessary JavaScripts from the SAME url the current page is of, it simply appends query-string parameters to it which will be recognized by the standard handler and the client-side scripts are returned. The problem is that: When ASP.NET page output caching is enabled and a request is issued to a cached page, there will be no server-side processing, only the cached raw HTML will be returned insted of the Javascript files.

By implementing a custom IHttpHandler to serve the Javascript files you can avoid this problem as the scripts are returned from a different URL. In my SharePoint scenario I used the CombineScriptsHandler from Loren and put it into the _layouts-Folder in the 12-hive. From then on I could turn on page output caching without any problems. This is the ToolkitScriptManager markup i’m using:

 <%@ Register TagPrefix=”act” Namespace=”AjaxControlToolkit” Assembly=”AjaxControlToolkit, Version=3.0.20820.25802, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e” %>

 <act:ToolkitScriptManager ID=”scriptManager” runat=”server” ScriptMode=”Release” CombineScripts=”true” CombineScriptsHandlerUrl=”/_layouts/swisscom/CombineAjaxToolkitScriptsHandler.ashx” AsyncPostBackTimeout=”320″ EnableHistory=”True” EnableScriptGlobalization=”true” EnableScriptLocalization=”true” EnablePartialRendering=”true” EnableViewState=”true” />

Leave a Reply