KR.Senthil’s .NET RECIPE

Blogging my professional and personal experiences in career and living

How To Inject Javascript during partial postback in ASP.Net

Posted by senthilsweb on December 15, 2007

Injecting Javascript Codes using ClientScriptManager class i.e. RegisterClientScriptBlock in asp.net codebehind will not work for partial postback [ASP.NET AJAX Calls].
To make it work we need to use ScriptManager class’s ScriptManager.RegisterStartupScript, ScriptManager.RegisterClientScriptBlock methods

Example:

string msg = string.Format(“alert(‘Test Javascript from partial postback’);”);
ScriptManager.RegisterStartupScript(TestButton, typeof(Button),”Test_my_script”,msg, true);

TestButton => The control which triggered the partial postback [It may be other controls which are inside the updatepanel]
typeof(Button) => System.Type object for a type
Test_my_script => any unique string as key
msg => Javascript code to be injected
true /false => Adds javascript script tags i.e. <script></script>

2 Responses to “How To Inject Javascript during partial postback in ASP.Net”

  1. NaReN said

    Instead of typeof(Button), this.GetType() can be used; and so need to remember that which control triggered the partial postback.

  2. Yuriy said

    If partial postback is caused by Infragistics controls, you need a slightly different approach: http://codecorner.galanter.net/2009/07/28/injecting-client-script-into-infragistics-async-postback/

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>