As early as ASP.NET 1.0, several means, such as Page.RegisterClientScriptBlock and Page.RegisterStartupScript,
were created to boost up the server controls with the capacities of client
side. In the current ASP.NET 2.0, the above two methods both became obsolete
and were replaced by class ClientScriptManager which
can be accessed using Page.ClientScript. One of the
important methods of Page.ClientScript is RegisterClientScriptBlock through which we can achieve the
goal of injecting client-side JavaScript blocks into object Page.
Therefore, we can succeed in populating the required data from the server side,
and thus ensure to locally update a web page.
About Method RegisterClientScriptBlock
There are two signatures for this method, as illustrated in
the following Listing.
Listing 5
ClientScriptManager.RegisterClientScriptBlock (Type type, String key,
String script);
ClientScriptManager.RegisterClientScriptBlock (Type type, String key,
String script, Boolean addScriptTags);
A client script is uniquely identified by its key and its
type. Scripts with the same key and type are considered duplicates. Only one
script with a given type and key pair can be registered with the page.
Attempting to register a script that is already registered does not create a
duplicate of the script.
Call the IsClientScriptBlockRegistered method to determine
whether a client script with a given key and type pair is already registered
and avoid unnecessarily attempting to add the script.
In this overload of the RegisterClientScriptBlock method,
you must make sure that the script provided in the script parameter is wrapped
in a <script> element block.
The RegisterClientScriptBlock method adds a script block to
the top of the rendered page. The script blocks are not guaranteed to be output
in the order they are registered. If the order of the script blocks is important,
use a StringBuilder object to gather the scripts together in a single string,
and then register them all in a single client script block.
In this article we are not going to dig further into the
related theory behind but concentrate on constructing an associated example to
show the usage of method Page.ClientScript.RegisterClientScriptBlock.
Author's Note: In ASP.NET AJAX there
is also a method RegisterClientScriptBlock which is provided by ASP.NET
Server control—ScriptManager. We can
use it to create the Server side Extender for ASP.NET AJAX.