Call JavaScript Function in C# from code behind


Call Javascript function from code behind C#:  This article explains on button click how to call  javascript function in C# . While developing web application there is a common need to call javascript function, based on some logic to do some further business logic . In calling JavaScript function from code-behind C# after button click [server-side] is a quite easy code.

About JavaScript function:
JavaScript function is a set of code inside a block, which gets execute on client side. To defined JavaScript function we use the function keyword, followed by a name, and then followed by parentheses ().

Now first, we add an Webform in our project and write a JavaScript function. Here default.aspx is our newly added Web page add a javascript function on default.aspx page under head tag, as later we want to call this JS function from code behind. Our simple javascript alert method function look like as written below. We call this alert method from c# codebehind.

<script type="text/javascript" language="javascript">
	function helloWorld(){
	   alert("welcome to");	

Two ways to call JavaScript function in C#

  1. Using ClientScript.RegisterStartupScript 
  2. Using ScriptManager.RegisterStartupScript ( If updatepanel is used)
Let see each method one by one.
Method 1: Use ClientScript.RegisterStartupScript to call javascript from code-behind Using the below-given code we able to call the javascript function from server-side.
ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);
As our javascript function name as helloWorld() so we write this code as shown below.
ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:helloWorld(); ", true);

Method 2: Use ScriptManager.RegisterStartupScript to call javascript from code-behind. If you have used UpdatePanel control from ajax toolkit in your web page then the code for calling javascript function would be like as written below.
ScriptManager.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);

As our javascript function name as helloWorld() so we write this code as.

ScriptManager.RegisterStartupScript(GetType(), "Javascript", "javascript:helloWorld(); ", true);

Conclusion: Here in this article we learn how in C#  using ClientScript.RegisterStartupScript  or  ScriptManager.RegisterStartupScript we can call any JavaScript function in C# from code-behind may be on page load or on button click.

Other Refernece:

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to thank me? 👳 Buy Me a Coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.

Post Comment

Your email address will not be published. Required fields are marked *


  1. Mick 05/19/2015 11:30:48
    Thanks i was finding it difficult to get javascript function from .cs page .