Call Javascript Function from C# code behind in Asp.net

satinder singh / Updated : / 10 Comments

Call Javascript function from code behind C#:  This article explains on button click how to call  javascript function from code behind C# . While developing web application there is a common need to call javascript function, based on some business logic to do some further business logic .

In Asp.net calling javascript function from code-behind c# after button click [server-side] is a quite easy code.

First, we add an Asp.net 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 codepedia.info");	
	}
</script>
//*

Two ways to call javascript function from 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 Asp.net 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 we learn how in Asp.net C#  using ClientScript.RegisterStartupScript  or  ScriptManager.RegisterStartupScript we can 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!

10 comments on “Call Javascript Function from C# code behind in Asp.net”

  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 *