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

发布于 2024-07-09 07:26:42 字数 212 浏览 6 评论 0原文

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

我在偶数方法中尝试了以下方法,但没有成功:

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

谜泪 2024-07-16 07:26:42

您应该使用 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);

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

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

The advantage with ScriptManager is that it works with asynchronous postbacks, so if you are using AJAX it will not work with the 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.:

ScriptManager.RegisterStartupScript(MainUpdatePanel, typeof(string), "ShowPopup", "showCheckOutPopIn('Livraison',556);", true);
傲世九天 2024-07-16 07:26:42

解决方案

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

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

发现:回复:回发后执行javascript

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

拿命拼未来 2024-07-16 07:26:42

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

当然,问题是,这个 javascript 应该做什么,而您不能通过某些属性或其他设置来在服务器端执行这些操作,这些属性或其他设置会反映在标记中?

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.

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?

鱼窥荷 2024-07-16 07:26:42

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

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

解决方案就是简单地使用 ScriptManager类函数:
<代码>
ScriptManager.RegisterStartupScript(this, typeof(Sections), "初始化", "initialize();", true);

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+

And solution is simply use ScriptManager class function:

ScriptManager.RegisterStartupScript(this, typeof(Sections), "Initialize", "initialize();", true);

—━☆沉默づ 2024-07-16 07:26:42

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

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文