繁体   English   中英

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

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

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

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

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

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

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

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

您的代码如下所示:

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

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

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

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

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

发现: Re:回发后执行javascript

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

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

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

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

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

也许迟到但你可以像这样使用

母版页:

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

网页表格:

 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