最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How do I call a JavaScript function from Asp.Net Code Behind? - Stack Overflow

programmeradmin9浏览0评论

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?

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?

Share Improve this question edited Sep 10, 2015 at 13:57 mason 32.7k11 gold badges83 silver badges126 bronze badges asked Sep 10, 2015 at 13:13 Antoine PelletierAntoine Pelletier 3,3164 gold badges41 silver badges68 bronze badges 1
  • stackoverflow.com/a/5325787/2777098 – display name Commented Sep 10, 2015 at 13:19
Add a comment  | 

4 Answers 4

Reset to default 8

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.

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

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

According to MSDN using this overload you have to wrap your call in <script></script> tags, so you would do:

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

or use the other overload

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

which will adds them or not depending on the boolean parameter.

发布评论

评论列表(0)

  1. 暂无评论