Monday, June 3, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
129
rated 0 times [  131] [ 2]  / answers: 1 / hits: 19257  / 9 Years ago, thu, september 10, 2015, 12:00:00

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?


More From » c#

 Answers
85

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.


[#65112] Tuesday, September 8, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anikas

Total Points: 258
Total Questions: 102
Total Answers: 95

Location: Monaco
Member since Sun, Jan 16, 2022
2 Years ago
anikas questions
;