Posted by: mdjastrzebski | July 20, 2009

ClientScriptManager vs. ScriptManager – comparison

In preparation for MCTS ASP.NET certification  I have been interested in ScriptManagera – ASP.NET component responsible, inter alia, for AJAX – and its relations with ClientScriptManager. In this article I wanted to characterize the similarities and differences between these classes. For this purpose, use is mainly a book (Self-Paced Training Kit – ASP.NET 3.5 by Mike Snell et al.) and the MSDN.

So Let’s get started!

Class ClientScriptManager

The class ClientScriptManager is responsible for keeping a record on the registred JS scripts. You can use it to register the script contained in a string as follows:

string myScript = "window.alert('hello world');";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript1", myScript, true);

This will add to the resulting script as follows:

<script>
//<![CDATA[
window.alert('hello world')//]]>
</script>

It is word adding an additional registration instructions to check if this key is not already included. In this way:

if(!Page.ClientScript.IsClientScriptBlockRegistered("MyScript1"))
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript1", myScript, true);

The second parameter is the name-key for the script, so that we can then unregister it, the third parameter is the the script, and the fourth parameter specifies whether the script is to be surrounded with <script> tags (true) or it already is surrounded (false). DThe exact intention of the first parameter is unknown. The book says only that usually there’s page type (like this.GetType ()) or some control type, of controls which are on the page.

Similarly, you can attach scripts contained in a separate file:

ClientScript.RegisterClientScriptInclude(this.GetType(), "MyScript2", "MyScript.js");

What generates in the results page, the following code:

<script src="Scripts/MyScript2.js"></script>

You can also add scripts contained in an assembly, which provides an easy way to deploy script on a server:

ClientScript.RegisterClientScriptResource(typeof(ClientScriptResourceLabel), "ClassLibrary1.MyScript3.js");

This method will generate code:

<script src="/WebResource.axd?d=cG32i4DUhY0FkE6w-OJyhh3lgbxiIlGSyGv2x6ydxrS6sKMdeuysHqGevl1v_HSoaxD4D_ap9HoehZc1Tl9_tQ2&amp;t=633837180148964845" type="text/javascript"></script>

You can see here a reference to HTTP handle WebResource.axd, which is responsible for the serving the resources made available out of the assembly file.

ScriptManager Class

Class ScriptManager, like ClientScriptManager, is responsible for managing user scripts, it has also other functions:  it includes the appropriate scripts to the page from the Microsoft AJAX Library and coordinates the partial page updates.

Simply attach the script by creating a reference to it in markup:

<asp:ScriptManager ID=”ScriptManager1” runat=”server”>
<Scripts>
<asp:ScriptReference Name=”MyScript.js” />
</Scripts>
</asp:ScriptManager>

Similarly, you can add the same to the code-behind:

ScriptReference scriptRef = new ScriptReference("Scripts/MyScript2.js");
ScriptManager1.Scripts.Add(scriptRef);

Scripts can be registered by the similar method of similard signature to ClientScriptManagera methods:

ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "MyScript", myScript, true);

Like the previous methods, here the arguments are: the type , the name-key, the content and whether the need to attach <script> tags . The first parameter is a control or page (function has two overloaded versions). If this is a control, then the script is to be sent to the page only if the control is updated during async postback. If the parameter is the page object,  a script will be sent with each async postback.

Summary

During the MCTS I had impression that the official (and simpler) intrepretation about usage of either the ClientScriptManagera or the ScriptManagera is such, that unless we already use at ScriptManager of AJAX or so, we should use the ClientScriptManager to register scripts (because it is lighter object, created just for that) but if you already use at ScriptManagera should use it to register the scripts.

ClientScriptManager

ScriptManager

Access to the object Always present in the code, available through Page.ClientScript You have to create a ScriptManager object  (you may do so in the code of ASP.NET pages). Single page may contain only 1 ScriptManager object – this includes both the MasterPages and the ASCX controls. In this case, you must use the ScriptManagerProxy object .
Registering scripts Allows you to attach your own scripts, which do not use the MS AJAX Library. Allows you to attach your own scripts (as well as benefiting from object-oriented features of MS AJAX Library)
Other Features Used by the ASP.NET framework to add a simple JS (eg causing postback events when they are not normally caused – when you select the checkbox) Used by the control AJAXowe. Addes the scripts of the MS AJAX Library, coordinates partial page updates.
Moment of script registration Recorded scripts are available after a full PostBack Recorded scripts are available after both the full and asnyc PostBack.
The method of registering scripts Methods:RegisterClientScriptBlock

RegisterClientScriptInclude

RegisterClientScriptResource

Registering skryts by ScriptReference object and the method Scripts.Add ()Registering script by methods similar to ClientScriptManager ones:

RegisterClientScriptBlock (without AJAX Library available)

RegisterClientScriptInclude RegisterClientScriptResource

ASP.NET Version Since ASP.NET 2.0 Since ASP.NET 3.5

Leave a comment

Categories