Tales from the Evil Empire

Bertrand Le Roy's blog

News

Ads Via DevMavens

ASP.NET AJAX UpdatePanel Control: Add Ajax interactivity to your ASP.NET 2.0 web pages


follow bleroy at http://twitter.com

Bertrand Le Roy




Add to Technorati Favorites

Blogs I read

My other stuff

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

Zubair.NET! said:

hi Bertrand,

As Rick Strahl puts in his editorial for Code magazine here www.code-magazine.com/Article.aspx that jQuery and AJAX can seemlessly work together without stepping on each other, we had a nightmare working with jQuery along with ASP.NET AJAX on a project, we ended up choosing jQuery because we had more fine-grain control over some of the neat client-side tricks but having said that we'd love to use ASP.NET AJAX for some of the built-in features that are a pain in the neck to build with jQuery, kinda best of both worlds so my question is, is there really a way that they both work together ?

# July 8, 2008 1:17 AM

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

Rick Strahl said:

Bertrand - I think one issue with this is that Intellisense stops working with any subclass of ScriptManager if you have script code in the page. Something about how the page is parsed for scripts that is dependent on the exact asp:ScriptManager reference.

I had previously tried subclassing script manager and overriding the method that injects the MS Ajax code. While that worked Intellisense didn't work.

Intellisense - especially from resources is something that is quite useful and I mentioned this to Jeff and was hoping that there'd be some sort of interface or documented syntax that can be applied to allow other controls to provide Intellisense.

# July 8, 2008 3:02 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

Rick Strahl said:

@Bertrand - I'm also very interested @Richard's comment and would love to hear the ASP.NET team address this. One of the big drawbacks of MVC I see is related to the inability to address the document and inject anything into it - including scripts. Which is crucial for building reusable - well, anything really.

# July 11, 2008 4:50 AM

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

Bertrand Le Roy said:

@fluxtah: yes, it should check if there is a script manager on the page and register it with it if there is one. There are lots of benefits in doing so. For example, your users will be able to relocate the script and combine it. But that won't be enough to give IntelliSense, the user will have to put the reference explicitly in ScriptManager in order to get IntelliSense in the page.

Visual Studio special-cases ScriptManager and queries a special API when it's there to get the list of script references. One thing you might be able to do is derive from ScriptManager and expose your script as a static script reference, as if it had been added by the user.

# October 7, 2008 1:30 PM

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 a little off topic, but since you mentioned that the script manager removes duplicates...

For some reason removing duplicates doesn't work with script combining.  I posted on the forum but no one could help.

forums.asp.net/.../2924346.aspx

Any ideas?

# February 12, 2009 1:32 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
Leave a Comment

(required) 

(required) 

(optional)

(required)