如何从 Asp.Net 代码隐藏调用 JavaScript 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32502897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I call a JavaScript function from Asp.Net Code Behind?
提问by Antoine Pelletier
This is my JavaScript function :
这是我的 JavaScript 函数:
function WantToSave()
{
alert('You should save now !');
}
And this is my ASP.NET code behind :
这是我背后的 ASP.NET 代码:
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?
该RegisterStartupScript
达到的功能,这是肯定的。但它不会触发我的 JavaScript 函数。第二个参数应该是“起始脚本的键”是的,但是我应该在那里放什么?
回答by mason
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.
默认情况下,脚本被写入输出但没有<script>
标签。如果您使用浏览器的 JavaScript 控制台或在客户端查看生成的 HTML,您可能会注意到这一点。确保您熟悉这些工具。
You can have it add the script tags for you with a slightly different overloadof 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.
键字符串和控件注册的类型的组合用于唯一标识注册的脚本,以防您以后想要取消注册或用不同的脚本替换它。因此,密钥不必是任何特定的东西,只要是独特的东西即可。
回答by John Paul
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.
您可以使用脚本管理器注册启动脚本方法。它有一个名为 add script tags 的 bool 参数(最后一个),它为您添加脚本标签。
ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "WantToSave();",true);
回答by n32303
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.
或者您应该在调用结束时添加一个 true 参数,它指定添加脚本标签。
Page.ClientScript.RegisterStartupScript(this.GetType(), "MyKey", "WantToSave();",true);
回答by Ben Robinson
According to MSDN using this overload you have to wrap your call in <script></script>
tags, so you would do:
根据使用此重载的 MSDN,您必须将调用包装在<script></script>
标签中,因此您可以执行以下操作:
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.
这将根据布尔参数添加或不添加它们。