简体   繁体   English

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

[英]Javascript Alert before redirecting in ASP.NET

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

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

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

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

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

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