Tuesday, September 11, 2007

ScriptManager - RegisterStartupScript


In days of old you would have added script to the page as a result of a full postback such as this:

protected void Button1_Click(object sender, EventArgs e)


Page.ClientScript.RegisterStartupScript(this.GetType(),"myscript","alert('hello world!');"); // or below

Page.RegisterStartupScript("CallClient", callClientScript);


But this new method can break under ASP.NET AJAX. Why? Because ClientScriptManager is not a control that understands partial postbacks and partial rendering, and so script code registered with it will not be included in the page response for a partial postback.

Instead, if you wanted the button to introduce client script into the page output, you would leverage the ScriptManager:

protected void Button1_Click(object sender, EventArgs e)


ScriptManager.RegisterStartupScript(this,this.GetType(),"myscript","alert('hello world!');",true);


The result is the same, but the method is slightly different. In fact, the methods of the ClientScriptManager control are now included in the ScriptManager control as static methods (RegisterStartupScript, for example). Whenever you are utilizing ASP.NET AJAX and want to work with script through partial postbacks, you must now use methods exposed by the ScriptManager control instead.

Thanks & Regards,

Arun Manglick || Tech Lead

1 comment: