调用 JS 警报或确认后的 Response.Redirect

发布于 2024-11-01 05:53:34 字数 278 浏览 4 评论 0原文

我正在开发 VB.NET Web 应用程序。当有人成功更改密码时,我想显示一条弹出消息,让他们知道密码已成功更改。他们单击“确定”后,我想将他们重定向到主页。代码如下所示:

ClientScript.RegisterStartupScript(Me.GetType(), "confirmScript", "ConfirmNewUser();", True)
Response.Redirect("MainPage.aspx")

为什么会发生重定向并且警报弹出窗口从不显示?

I am working on a VB.NET web application. When someone successfully changes their password I want to show a popup message that lets them know it was changed successfully. After they click OK I want to redirect them to the main page. Code looks like this:

ClientScript.RegisterStartupScript(Me.GetType(), "confirmScript", "ConfirmNewUser();", True)
Response.Redirect("MainPage.aspx")

Why does the redirect happen and the alert popup never displays?

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

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

发布评论

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

评论(4

北渚 2024-11-08 05:53:34

试试这个:

1)从后面的代码中删除Response.Redirect。

2)更改ConfirmNewUser函数,如下所示:

function ConfirmNewUser(){
    //Existing Code of ConfirmNewUser
    //New Code.
    var msg = "Password changed successfully. Press OK to go to Home page Cancel to stay on current page.";
    if(confirm(msg)){
        window.location.href = "MainPage.aspx";
    }

}

Try this:

1) Remove Response.Redirect from the code behind.

2) Change the ConfirmNewUser function as given below:

function ConfirmNewUser(){
    //Existing Code of ConfirmNewUser
    //New Code.
    var msg = "Password changed successfully. Press OK to go to Home page Cancel to stay on current page.";
    if(confirm(msg)){
        window.location.href = "MainPage.aspx";
    }

}
顾冷 2024-11-08 05:53:34

您正在调用重定向服务器端,您的脚本永远没有机会运行。使用 window.location 进行客户端重定向,如下所示:

function ConfirmNewUser() {
  if(confirm("Your password has been changed, click OK to continue")) {
    window.location = "MainPage.aspx"; //go to home page
  }
}

You are calling the redirect server side, your script never get a chance to run. use window.location to do the redirect client side, something like this:

function ConfirmNewUser() {
  if(confirm("Your password has been changed, click OK to continue")) {
    window.location = "MainPage.aspx"; //go to home page
  }
}
天邊彩虹 2024-11-08 05:53:34

原因是所有服务器端处理都会先于客户端进行。

一种解决方案是将“MainPage.aspx”传递给您的客户端脚本,如下所示:

ConfirmNewUser('MainPage.aspx');

然后您的客户端脚本必须采用 URL 参数:

function ConfirmNewUser(url) { ... }

并跟进 window.location:

...
if(confirm(...))
{
  window.location = url;
}

并从服务器代码中删除以下内容:

Response.Redirect("MainPage.aspx")

The reason is because all server-side processing will take place prior to client-side.

One solution would be to pass "MainPage.aspx" to your client script as follows:

ConfirmNewUser('MainPage.aspx');

Your client script would then have to take a URL parameter:

function ConfirmNewUser(url) { ... }

and follow up with a window.location:

...
if(confirm(...))
{
  window.location = url;
}

and remove the following from your server code:

Response.Redirect("MainPage.aspx")
浅沫记忆 2024-11-08 05:53:34

Response.Redirect 设置 Location http 标头和 302-Moved 响应,浏览器一看到它就会对此采取行动。由于标题位于内容之前,因此您的脚本永远不会被看到或解析。

Response.Redirect sets the Location http header and a 302-Moved response, the browser will act upon this as soon as it sees it. As headers come before content, your script is never seen or parsed.

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