Using ClientScriptManager in C#

We know that we all are using the script tag for JavaScript function in our web page. In the case of some situations we need to add the scripts in code behind by dynamically. So .Net Framework 2.0 introduced a class namely “ClientScriptManager” that is used to manage script in code behind.

Now we are going to about the ClientScriptManager class and its usage with some sample codes.

ClientScriptManager:

This class is used to manage client scripts in .Net. We can get the object of this class from Page.ClientScript. We need to register the client script by using the method of RegisterClientScriptBlock () which is from that object of ClientScriptManager.

The ClientScriptManager class uniquely identifies scripts by a key String and a Type. Scripts with the same key and type are considered duplicates. Using the script type helps to avoid confusing similar scripts from different user controls that might be in use on the page.

The ClientScriptManager class can be used to invoke client callbacks in situations when it is desirable to run server code from the client without performing a postback. This is referred to as performing an out-of-band callback to the server. In a client callback, a client script function sends an asynchronous request to an ASP.NET Web page. The Web page runs a modified version of its normal life cycle to process the callback. Use the GetCallbackEventReference method to obtain a reference to a client function that, when invoked, initiates a client callback to a server event.

(This is from MSDN)

Let we watch this in Code behind. Just use the following code in page load and run it.

1. RegisterClientScriptBlock()

In Page_Load,

protected void Page_Load(object sender, EventArgs e)
{
//Get the ClientScript property as an object of ClientScriptManager from Page object.
ClientScriptManager client = this.Page.ClientScript;
//Check the script block is not already registered
if (!client.IsClientScriptBlockRegistered(this.GetType(), "Alert"))
{
client.RegisterClientScriptBlock(this.GetType(), "Alert", "alert('Welcome !')", true);

2. RegisterStartupScript()

RegisterClientScriptBlock and RegisterStartupScript both the methods are using to fire during the startup of page. But the difference is that the RegisterClientScriptBlock method will fire the script “after the form open tag but before the page controls”. And the RegisterStartupScript method will fire the script “after page controls but before the form close tag”. So the first method won’t get the controls value in run time.

For example, we are having a label box with the text “Vinoth” in a form, and then call the script as follows,

ClientScriptManager client = this.Page.ClientScript;
if (!client.IsStartupScriptRegistered(this.GetType(), "Alert"))
{
client.RegisterStartupScript(this.GetType(), "Alert", "alert(document.getElementById('lblName.Text'))", true);
}

...S.VinothkumaR.

3 comments:

Unknown said...

THANK you, finally a working example.

Cheers Michel.

Unknown said...

Thanks! This one really works. No other code posted on other sites actually worked.

Shivakumar Naik said...

really simple and great explanation.. superb