Call Javascript Function from Code Behind in C#

Call Javascript function from code behind C#:  This article explains how to call  javascript function from code behind in C# on button click or based upon some condition. While developing application there are many times where we want to call a javascript function from C#, based on some business logic we do further logic etc. In calling javascript function from code-behind c# after button click [server-side] is a quite easy code, as shown below.

First, we add an Webform page in our project and write a javascript function on our .aspx page, 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 JS function alert method look like as written below.

Code to call javascript function from C# on pageLoad

Using the below-given code we able to call the javascript function from server-side.

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

If you have used UpdatePanel control from ajax toolkit in your design web page then the code for calling javascript function would be like as written below .i.e using ScriptManager.RegisterStartupScript

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

Conclusion: This is how in C# we able to call any javascript function from code-behind may be on page load or on button click. Hope you enjoyed this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!


  1. Thank you.

    ??how to pass arguments to the javascript function being called.

    ??returning a result from the javascript function.

    Thank you.

    • Hello Domingo,

      For passing arguments / parameters you can simply pass value to the JS function from code-behind C#

      For example :
      Code behind:
      string strParamter = "Hello World test";
      ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:helloWorld('"+strParamter+"'); ", true);

      Client side (.aspx):
      function helloWorld(data) {