[email protected]      beta
Donate

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! 

Share:    
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++ ; }


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

  1. Brennan Young

    Very useful, thanks.

    Just want to mention that this will fail if your code behind (i.e. Response) is returning a download instead of an html page for display in the browser. Obvious perhaps. But it wasn’t to me at first.

  2. Pratik

    ScriptManager.RegisterStartupScript(Me, [GetType](), “Javascript”, “javascript:Sch3Collapse(); “, True)

    does not work on postback

    • Satinder singh

      Hi Pratik,
      The above code worked for me, that is I able to call javascript function whenever I clicked asp.net button control. Am not sure how you used above code. But if you have added update panel from ajax toolkit, then you need to add below script at the bottom of your Web page ( .aspx page)

       
      <script type="text/javascript">
         Sys.WebForms.PageRequestManager.getInstance().add_endRequest(InIEvent);
      </script>
      

      <script>
      function InIEvent(){
        
       function fn_1(){
          // client side function here
        }
       
      function fn_2(){
          // client side function here
        }
      
      }
      </script>
  3. 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 *