Tales from the Evil Empire

Bertrand Le Roy's blog

News


Bertrand Le Roy

BoudinFatal's Gamercard

Tales from the Evil Empire - Blogged

Blogs I read

My other stuff

Archives

Using ScriptManager with other frameworks

ScriptManager is a useful control. It manages script references, removes duplicates, enables localization and debug/release modes, enables script combining and makes client-side component-based development easier (components register their script dependencies with ScriptManager without the page developer having to know those dependencies). But one thing we should have anticipated was that this control would be interesting to developers who wish to use a different framework than the Microsoft Ajax Library.

Currently, ScriptManager isn't really usable without Microsoft Ajax because it includes the Microsoft library (without a simple way to disable it) and outputs some startup script to the page. We're looking at correcting that in future versions of the framework, but in the meantime I wanted to come up with a workaround.

The workaround is a simple control derived from ScriptManager and that enables the replacement of the MicrosoftAjax.js reference with a script of your choice:

<%@ Register Namespace="Bleroy" TagPrefix="asp" %>
...
<
asp:AltScriptManager runat="server"
FrameworkPath="~/script/jquery-1.2.6.js" />

Here, we're replacing Microsoft Ajax with jQuery. Being derived from ScriptManager, AltScriptManager benefits from all its features. It just removes the Microsoft Ajax stuff. The resulting markup is:

<script src="script/jquery-1.2.6.js" type="text/javascript"></script>

The first thing the control does is set the default value for EnablePartialRendering to false. This suppresses the default reference to MicrosoftAjaxWebForms.js.

Then, it registers from Init two empty scripts with the type of the ScriptManager and names "AppInitialize" and "FrameworkLoadedCheck". This is a bit of magic that relies on knowledge of the names and types that ScriptManager uses to register its inline scripts.

Finally, it adds a ScriptReference with the Microsoft Ajax assembly and the "MicrosoftAjax.js" name but with a different path (the value of the new FrameworkPath property). This effectively replaces the Microsoft Ajax script reference with a script of your choice.

This should broaden developer choice in terms of client framework while keeping the great benefits of ScriptManager. Hope that helps.

Download it from here: http://weblogs.asp.net/bleroy/attachment/6374028.ashx

Comments

Bertrand Le Roy said:

@Zubair: I'd be very interested to know what didn't work well for you with jQuery and ASP.NET Ajax. Can you please drop me an e-mail at bleroy at microsoft?

# July 8, 2008 1:30 AM

Zubair.NET! said:

I don't have a particular case with me at the moment but I know we had issues and I didn't want to give up ASP.NET AJAX but my UI dev insisted on jQuery, it was a hard decision to make at the end of the day between built-in controls of ASP.NET AJAX vs more control in jQuery for some other tricks.

I'd love to see an example of both these frameworks working side by side in a project, can you put up a post with some sample ? that'll be great. Thanks

# July 8, 2008 1:54 AM

Matt Brooks said:

Given that scripts from other libraries (e.g. jQuery) will not include calls to:

Sys.Application.notifyScriptLoaded();

What affects will this have at runtime? I was under the impression that it was a requirement for scripts to incude this call in order to play nice with ScriptManager - or is this ONLY a requirement when using partial loading, which you mention gets disabled by default anyway?

# July 8, 2008 3:49 AM

Bertrand Le Roy said:

@Rick: true, but I think the control is still quite useful. I'll follow-up with Jeff.

@Matt: the script loaded notification really is to play nice with UpdatePanel's script loader. If your scripts are never added to the page as part of a partial update, you don't need it (but having it won't hurt if you include the full code that tests for Sys before using it).

# July 8, 2008 2:02 PM

Bertrand Le Roy said:

@Rick: Jeff tells me this is fixed in SP1. Couldn't get it to work on SP1 Beta 1 though. Still investigating.

# July 8, 2008 3:33 PM

Peter said:

I appreciate you looking into this, but let me raise the red flag for the record.

I would love to be able to plaster a poster on every ASP.NET team member's office "Do not hardcode! Make it generic!" The SM issue is one, hardcoding intellisense behavior is another example. There are other examples of built-in magic that just smell of hardcoding.

And another thing: Can we please stop coming out with new stuff, take a deep breath, and actually go back and try to polish some more on the things people actually use?! And how about actually going back and updating some of the "old" stuff. I know it's vague (I can come up with examples if needed), but I feel ASP.NET is getting dirty and need to be cleaned.

Rant over,

Thanks.

# July 10, 2008 12:29 AM

Bertrand Le Roy said:

@Peter: feel free to contact me at bleroy at microsoft to tell me what needs to be cleaned.

# July 10, 2008 12:40 AM

RichardD said:

The most annoying feature of the ScriptManager control is that it is completely useless without a server form.

Although it's possible to include the ScriptManager control in a page without a server form (when EnablePartialRendering is false), none of the registered scripts will be output to the page.

The control seems to rely on the ClientScriptManager class, which only outputs the registered scripts when the internal Begin/EndFormRender methods of the Page call internal methods on the ClientScriptManager class.

Since MVC views can't use server forms, this must surely be on the "TODO" list?

# July 10, 2008 2:51 PM

Bertrand Le Roy said:

@Richard: Yes :)

# July 10, 2008 3:45 PM

Bertrand Le Roy said:

@Rick: I think you're asking a different question here, probably a more general one. If you want, we can start an e-mail discussion with Phil about that?

# July 11, 2008 2:34 PM

Ben said:

Good point Richard.

Thanks

# July 20, 2008 1:16 AM

fluxtah said:

Does anybody know how ScriptManager gets intellisense working for MicrosoftAjax.js?

I have a control called SnippetManager that simply registers a javascript file and nothing much else, I wanted to make it easy for someone to include the script by drag drop just like ScriptManager but it does not exhibit the intellisense enabling magic.

I might have to just stick 'register SnippetManager.js with ScriptManager' in the instructions and forget wrapping it up in a control.

# October 7, 2008 6:34 AM

Fluxtah said:

Thanks for the answer Bertrand, I hope Visual Studio will support intellisense for ScriptControl's without having to additionally reference them with ScriptManager in the future.

# October 7, 2008 4:04 PM

CaitNY said:

This is great by the way, but it doesn't add the js file specified by FrameworkPath into the CompositeScripts...?

# February 12, 2009 2:35 PM

Bertrand Le Roy said:

@CaitNY: I'll look at that forum post. If you're using CompositeScript, you should be able to modify the source code so that the new reference is added to the composite list instead. The source code isn't that complex, that should be relatively easy to do. Let me know how that goes.

# February 12, 2009 2:54 PM

Jeff Hegedus said:

Nothing new on this thread in a while.  Has anything changed?  Is it possible now to subclass ScriptManager without loosing intellisense?

# September 3, 2009 8:25 PM

Bertrand Le Roy said:

In 4.0, you don't need to subclass ScriptManager to do that sort of thing. I t's handled natively.

# September 3, 2009 8:30 PM

Bertrand Le Roy said:

@Christofer: you are SO on your own on this one :)

# November 30, 2009 2:50 PM

Christopher Rannow said:

  Though I don't know why, it worked after I saved everything, exited visual studio and reopened my project. If you'd like I can e-mail you the VB version to post if you think others would find it useful.

# December 2, 2009 4:54 PM

Bertrand Le Roy said:

@Vladimir: no, script tags block the execution of the page, so if you put them in head, you are blocking the rest of the page from displaying until they have loaded, which is usually not what you want. It's nice and tidy to put the script tags in head but it's usually not the best option in terms of performance. Plus, script tags are valid pretty much anywhere on the page. For script manager, it's actually not possible the way it's built currently as it relies on the form runat=server to display, which is after the head tag. The best option is actually to use a script loader like the one you can find in ASP.NET Ajax 4.0.

# December 23, 2009 4:54 PM

Bertrand Le Roy said:

@Saeed: care to look at the date for this post?

# May 28, 2011 3:25 AM