【问题标题】:Displaying a server-side alert message and then redirecting to a new page显示服务器端警报消息,然后重定向到新页面
【发布时间】:2011-08-16 00:29:34
【问题描述】:

我的页面上有一个表单,用户可以在其中发送电子邮件,

        try
        {
            smtpClient.Send(message);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Key", 
                "alert('Thank you for your submission.');", true);
        }
        catch(Exception)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Key", 
                "alert('There was an error processing your request.');", true);
        }

        Response.Redirect("home.aspx");

警报不会出现,页面只是立即重定向,除非我注释掉 Response.Redirect,否则警报会起作用。我想显示警报然后重定向。

如何做到这一点?我想以某种方式使用计时器,但我不确定这是否是最好的解决方案。

谢谢。

【问题讨论】:

    标签: c# asp.net response registerclientscriptblock


    【解决方案1】:

    让 JS 做重定向,而不是使用 Response.Redirect。

    例如。

    alert('....'); window.location='home.aspx';
    

    【讨论】:

      【解决方案2】:

      原因是RegisterClientScriptBlock 调用和重定向代码在同一个页面生命周期内。这意味着 Response.Redirect() 在页面呈现给客户端浏览器之前被调用。客户端无法看到警报消息。

      解决方案是按照 Will 的建议使用 JS 重定向页面

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-08
        • 1970-01-01
        • 2018-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-27
        • 2018-07-20
        相关资源
        最近更新 更多