How do I call a JavaScript function from Asp.Net Code Behind?

15,215

Solution 1

By default, the script is written to the output but without the <script> tags. You probably would have noticed this if you were using your browser's JavaScript console or looking at the resulting HTML on the client. Make sure you familiarize yourself with those tools.

You can have it add the script tags for you with a slightly different overload of the method.

Page.ClientScript.RegisterStartupScript(this.GetType(), "MyKey", "WantToSave();", true);

Or you can add them yourself:

Page.ClientScript.RegisterStartupScript(this.GetType(), "MyKey", "<script>WantToSave();</script>");

The combination of the key string and the type the control was registered with serve to uniquely identify the registered script, in case you later want to unregister it or replace it with a different script. So the key doesn't have to be anything specific, just something unique.

Solution 2

You can use script manager register startup script method. It has a bool parameter (last one) called add script tags which add the script tags for you.

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "WantToSave();",true); 

Solution 3

You should try this:

var name = "WantToSave";
var script = "<script>alert('You should save now !');</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), name, script);

Or you should add a true parameter at the end of your call, which specifies adding script tags.

Page.ClientScript.RegisterStartupScript(this.GetType(), "MyKey", "WantToSave();",true);
Share:
15,215
Antoine Pelletier
Author by

Antoine Pelletier

I'm a web developer, and a web app manager. I mostly use ASP.Net, MVC, C#, Entity Framework, SQL server and Ajax. I'm a french Canadien, so yes, I do play hockey :)

Updated on June 10, 2022

Comments

  • Antoine Pelletier
    Antoine Pelletier almost 2 years

    This is my JavaScript function :

    function WantToSave()
    {
        alert('You should save now !');
    }
    

    And this is my ASP.NET code behind :

    Page.ClientScript.RegisterStartupScript(this.GetType(), "MyKey", "WantToSave();");
    

    The RegisterStartupScript function is reached, that's sure. But it won't trigger my JavaScript function. And that second parameter, it is supposed to be a "Key of the starting script" yes but what should I put there?

  • Antoine Pelletier
    Antoine Pelletier over 8 years
    Thanks @mason , what about "MyKey" then, i could have wrote anything there ? why ?
  • mason
    mason over 8 years
    @AntoinePelletier Correct. The combination of the key string and the type the control was registered with serve to uniquely identify the registered script, in case you later want to unregister it or replace it with a different script. So the key doesn't have to be anything specific, just something unique.
  • Antoine Pelletier
    Antoine Pelletier over 8 years
    This is quite interesting ! especially for the Guid.NewGuid() function wich allow me to show the alert as often as i want. thanks @Jhon Paul
  • SaiBand
    SaiBand over 3 years
    Thanks @mason - This is what I needed.