C# 在 ASP.NET 中重定向之前的 Javascript 警报

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

Javascript Alert before redirecting in ASP.NET

c#javascriptasp.netajaxupdatepanel

提问by Rohit Chaudhari

I am using following code to display message while updating in update panel

我正在使用以下代码在更新面板中更新时显示消息

string jv = "alert('Time OutAlert');";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", jv, true);

It works fine.

它工作正常。

But when I use Redirect after it it loads the page without displaying the message. I want user to see the message and after clicking on "ok" it should redirect.

但是当我在它之后使用重定向时,它会加载页面而不显示消息。我希望用户看到消息,点击“确定”后,它应该重定向。

string jv = "alert('Time OutAlert');";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", jv, true);
Response.Redirect("~/Nextpage.aspx");

采纳答案by Vishal Suthar

Display the alert with javascript and then do the redirect with the same:

使用 javascript 显示警报,然后使用相同的方法进行重定向:

ScriptManager.RegisterStartupScript(this,this.GetType(),"redirect",
"alert('Time OutAlert'); window.location='" + 
Request.ApplicationPath + "Nextpage.aspx';",true);

回答by Aristos

You can not do that, the way you try because the message is running on the client side, but you make the redirect on code behind before the page loading to show the message.

你不能这样做,你尝试的方式是因为消息在客户端运行,但是你在页面加载之前对后面的代码进行重定向以显示消息。

The way to do that is to call right after the message a client side redirect as:

这样做的方法是在消息之后立即调用客户端重定向为:

window.location = "NextPage.asps";

回答by Ashish

This Works fine

这工作正常

                string message = "Upadate Successfull !!";
                string url = "/Post.aspx";
                string script = "{ alert('";
                script += message;
                script += "');";
                script += "window.location = '";
                script += url;
                script += "'; }";
                ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "alert", script, true);