在没有Ajax的asp.net回发后执行javascript函数

时间:2021-10-31 19:27:18

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

我希望在asp.net回发后使用ajax执行一个javascript函数。

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

我用偶的方法尝试了下面的方法,但运气不佳:

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

5 个解决方案

#1


46  

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

您应该使用ScriptManager类,因为该页面。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替换页面类方法来管理不赞成的脚本。参考:MSDN页。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代码,您希望在多个回发上执行该代码,那么您应该在firstargument中引用您的UpdatePanel,例如:

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

#2


8  

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

#3


0  

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.

我不太清楚页面的语法和用法是什么。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应该做什么,而不能仅仅通过某个属性或其他在标记中反映的设置在服务器端做什么?

#4


0  

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 + +跑步+ javascript +在+ +部分+回发+

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

而解决方案就是使用ScriptManager类函数:ScriptManager。RegisterStartupScript(typeof(部分),“初始化”、“初始化();,真正的);

#5


0  

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 :

Web表单:

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

#1


46  

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

您应该使用ScriptManager类,因为该页面。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替换页面类方法来管理不赞成的脚本。参考:MSDN页。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代码,您希望在多个回发上执行该代码,那么您应该在firstargument中引用您的UpdatePanel,例如:

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

#2


8  

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

#3


0  

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.

我不太清楚页面的语法和用法是什么。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应该做什么,而不能仅仅通过某个属性或其他在标记中反映的设置在服务器端做什么?

#4


0  

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 + +跑步+ javascript +在+ +部分+回发+

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

而解决方案就是使用ScriptManager类函数:ScriptManager。RegisterStartupScript(typeof(部分),“初始化”、“初始化();,真正的);

#5


0  

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 :

Web表单:

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