C# 在没有 Ajax 的情况下在 asp.net 回发后执行 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/320999/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:06:55  来源:igfitidea点击:

Execute javascript function after asp.net postback without Ajax

c#asp.net.net-2.0

提问by Alexandre Brisebois

I wish to execute a javascript function after asp.net postback with out using ajax.

我希望在不使用 ajax 的情况下在 asp.net 回发后执行 javascript 函数。

I've tried the following in my even method with no luck:

我在我的 even 方法中尝试了以下方法但没有运气:

Page.ClientScript.RegisterStartupScript(GetType(), "ShowPopup", "showCheckOutPopIn('Livraison',556);");

采纳答案by reSPAWNed

You should rather use the ScriptManager class, since the Page.ClientScript property is deprecated...

您应该使用 ScriptManager 类,因为不推荐使用 Page.ClientScript 属性...

The ClientScriptManager class is new in ASP.NET 2.0 and replaces Page class methods for managing scripts that are now deprecated.
Reference: MSDN - Page.ClientScript Property

ClientScriptManager 类是 ASP.NET 2.0 中的新增类,它取代了用于管理现在已弃用的脚本的 Page 类方法。
参考:MSDN - Page.ClientScript 属性

The advantage with ScriptManager is that it works with asynchronous postbacks, so if you are using AJAX it will not work with the ClientScriptManager.

ScriptManager 的优点是它可以与异步回发一起使用,因此如果您使用 AJAX,它将无法与 ClientScriptManager 一起使用。

Your code would look like this:

您的代码如下所示:

ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowPopup", "showCheckOutPopIn('Livraison',556);", true);

Note also that if you are using AJAX and have a piece of javascript code, that you want executed on multiple postbacks, then you should refer to your UpdatePanel in the firstargument e.g.:

另请注意,如果您使用 AJAX 并有一段 javascript 代码,您希望在多个回发上执行,那么您应该在第一个参数中引用您的 UpdatePanel 例如:

ScriptManager.RegisterStartupScript(MainUpdatePanel, typeof(string), "ShowPopup", "showCheckOutPopIn('Livraison',556);", true);

回答by Joel Martinez

I don't remember offhand what is the exact syntax/usage for the Page.ClientScript stuff ... that looks like it should work offhand. But if push comes to shove, just have a simple user control that you can enable/disable dynamically that will write out a javascript method in script blocks after the postback. When the page loads, this script execute whatever functionality you wish to execute.

我不记得什么是 Page.ClientScript 东西的确切语法/用法......看起来它应该可以立即工作。但是,如果要推送,只需有一个简单的用户控件,您可以动态启用/禁用它,它将在回发后在脚本块中写出一个 javascript 方法。当页面加载时,此脚本执行您希望执行的任何功能。

of course, the question is then, what is it that this javascript should do that you couldn't just do on the serverside via some property or other setting that would reflect in the markup?

当然,问题是,这个 javascript 应该做什么,而你不能通过一些属性或其他会反映在标记中的设置在服务器端做?

回答by Alexandre Brisebois

Solution

解决方案

I needed to add the script tags using the following overload.

我需要使用以下重载添加脚本标签。

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", "alert('Success!');", true);

Found : Re: execute javascript after postback

发现 : Re: 回发后执行 javascript

回答by Dhananjay

This person had same problem i think and solution by him worked for me too :

这个人有同样的问题,我认为他的解决方案也对我有用:

http://forums.asp.net/t/1121450.aspx?HOW+TO+run+javascript+on+each+partial+postback+

http://forums.asp.net/t/1121450.aspx?HOW+TO+run+javascript+on+each+partial+postback+

And solution is simply use ScriptManager class function: ScriptManager.RegisterStartupScript(this, typeof(Sections), "Initialize", "initialize();", true);

解决方案是简单地使用 ScriptManager 类函数: ScriptManager.RegisterStartupScript(this, typeof(Sections), "Initialize", "initialize();", true);

回答by Alican Kablan

Maybe late but you can use like this

也许晚了,但你可以这样使用

Master Page :

母版页:

   ScriptManager.RegisterStartupScript(this, typeof(Page),
            "ShowRegister", string.Format(@"$( document ).ready(function() {{ShowErrorMessage('{0}');}});", message), true);true);

Web Form :

网页表格:

 Page.ClientScript.RegisterClientScriptBlock(GetType(), "MEssage",
                          "$( document ).ready(function() {
ShowMessage('{0}');", true);