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() 

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, Senior Consultant, Microsoft .NET and Web Technologies