This article would help you in executing the JavaScript code after Server code execution finished in different scenario’s Like,
- Register JavaScript Code on the server side when your page is not having the update panel.
- Register JavaScript Code on the server side when you use update panel in your page.
Register JavaScript Code on the server side when your page is not having the update panel
You may be having a scenario where you want to open a new aspx page when user clicked on some button, if it’s mere client side operation then you can call onClientClick function, but some times it’s required to execute some server side code and then open the new page. This means you have first execute your server side code and then have to execute java script code. In this scenario by emitting our Client side code to page response object we can achieve the result.
you can emit the client side code to page response using the following code in server side.
Page.RegisterStartupScript("someUniqueKey","your JavaScript Code Here");
Example:
var script = "<script language=javascript>alert('ServerSideCodeFinished');</script>"
Page.RegisterStartupScript("someUniqueKey",script );
Even you can call your JavaScript functions which are having the entire functionality by sending some parameter to the function. But when you use update panel in your page, this code do not work. As update panel only updates the content of the content template it’s doesn’t bother about executing this code. so then how can we execute JavaScript code after server side code when using Update Panel?
Register JavaScript Code on the server side when you use update panel in your page
Try using the above code even in this case ( having update panel in the page), you will find nothing is firing. so change your code to
System.Web.UI.ScriptManager.RegisterStartupScript(UpdatePanelID, UpdatePanelID.GetType(), "uniquekey",<script type='text/javascript'>jsFunctionName(" + par1 + "," + par2 + ")</script>, false);
to get your JavaScript code run. Above i am registering the JavaScript code to Response object of the update panel. so when the Content template reloaded of the given update panel id, this code get registered.
[...] label. Instead of message if you want to run some client side script then read my previous article Register JavaScript Code on the server side when you use update panel in your page. This article will explain you to execute your client side script after server side code get [...]