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.


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);


How do I change the color of the title bar in a Win Forms .Net app using c#?

We can use the WM_NCPAINT Message to do that. We must have to code for override the WndProc method to change the color of title bar in a Win Forms. Here I have done that with the following code. You can do that by using the code below.

using System.Runtime.InteropServices;

static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);

private static extern IntPtr GetWindowDC(IntPtr hWnd);

protected override void WndProc(ref Message m)
base.WndProc(ref m);
const int WM_NCPAINT = 0x85;
if (m.Msg == WM_NCPAINT)
IntPtr hdc = GetWindowDC(m.HWnd);
if ((int)hdc != 0)
Graphics g = Graphics.FromHdc(hdc);
g.FillRectangle(Brushes.Green,new Rectangle(0,0,300,22));
ReleaseDC(m.HWnd, hdc);

That’s it…