简体   繁体   English

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

[英]Execute javascript function after asp.net postback without Ajax

I wish to execute a javascript function after asp.net postback with out using ajax. 我希望在asp.net回发之后执行一个javascript函数,而不使用ajax。

I've tried the following in my even method with no luck: 我在偶数方法中尝试了以下方法但没有运气:

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

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. ClientScriptManager类是ASP.NET 2.0中的新增类,它取代了用于管理现已弃用的脚本的Page类方法。
Reference: MSDN - Page.ClientScript Property 参考: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 eg: 另请注意,如果您使用的是AJAX并且有一段javascript代码,您希望在多个回发中执行,那么您应该在第一个参数中引用您的UpdatePanel,例如:

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

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

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

I don't remember offhand what is the exact syntax/usage for the Page.ClientScript stuff ... that looks like it should work offhand. 我不记得Page.ClientScript内容的确切语法/用法是什么......看起来它应该可以随意工作。 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. 但是如果push推进,只需要一个简单的用户控件,你可以动态启用/禁用,这将在回发后在脚本块中写出一个javascript方法。 When the page loads, this script execute whatever functionality you wish to execute. 页面加载时,此脚本会执行您希望执行的任何功能。

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应该做什么,你不能只在服务器端通过一些属性或其他设置反映在标记?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM