How to execute JavaScript Function From ASP.NET Code Behind


You have a JavaScript function and you want to execute it from ASP.NET code behind file. You can achieve this by using RegisterStartupScript method of ClientScript class as shown below:

Let's suppose you have following JavaScript function in your code.

<script type="text/javascript"> 
    function SayMeHello() 
    { 
         alert('Hello'); 
    } 
</script>

In order to call it from code behind, use the following code

if (!ClientScript.IsStartupScriptRegistered("helloAlert"))
{
    Page.ClientScript.RegisterStartupScript( this.GetType(), "helloAlert", "SayMeHello();", true);
} 

 

 

About author


User avatar

Waqas Anwar

Founder of EzzyLearning.com, Senior Consultant, Microsoft .NET and Web Technologies

Comments