Call Javascript Function from Code Behind in Asp.net C#

Call Javascript function from code behind C#:  This article explains how to call  javascript function from code behind in Asp.net C# on button click or based upon some condition. While developing asp.net 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 Asp.net calling javascript function from code-behind c# after button click [server-side] is a quite easy code, as shown below. First, we add an Asp.net 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.

//*
<script type="text/javascript" language="javascript">
	function helloWorld(){
	   alert("welcome to codepedia.info");	
	}
</script>
//*

Code to call javascript function from C# on pageLoad

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

//*
ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:helloWorld(); ", true);
//*

If you have used Asp.net 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

//*
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: This is how in Asp.net 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! 

Sharing is Sexy:    
Satinder Singh

Hi all, am Founder and Author of Codepedia.info, also atypical polyglot programmer from Bombay, India. I have a passion to create, solve, and deploy software applications.
       ForEach ( minute in MyLife ) { myExperience++ ; }


5 comments on “Call Javascript Function from Code Behind in Asp.net C#”

  1. Domingo Morales

    Thank you.

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

    ??returning a result from the javascript function.

    Thank you.

    • Satinder singh

      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) {
      alert(data);
      }


Post Comment


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