How To Inject Javascript during partial postback in ASP.Net

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 thoughts on “How To Inject Javascript during partial postback in ASP.Net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s