Sometimes you want to call javascript finctions from code behind.In this case you would use ClientScriptManager to do this.Let’s see an example.Here we would call a javascript function which shows an alert.Create a function in javascript ShowAlert().On Page_Load we will call this function.
Desgin code
<html>
<head runat="server">
<title></title>
<script type="text/javascript">
function ShowAlert() {
alert('This is being called from code behind');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
<p><a href="http://www.dotnetplace.com">ASP.NET controls with jquery</a></p>
</body>
</html>
Code behind
Now we will call the above javscript method from server side by using RegisterStartupScript
protected void Page_Load(object sender, EventArgs e)
{
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(this.GetType(), "script", "ShowAlert();", true);
}
Here you would have noted that the RegisterStartupScript method of ClientScriptManager class accepts four parameters.
- Control that registers the Script. Here page is registerig the script.
- This could be any key you want to write but make sure another script is not registered with this key.
- The script to be executed.Here you can write name of method to be executed or you can write script directly.
- Script tags to be added.true for yes false for not.
Microsoft Reference
ClientScriptManager.RegisterStartupScript Method
Ouput

Download
Call-javascript-function-from-server.zip (980.00 bytes)
Demo
See live demo