【问题标题】:alert followed by a Response.Redirect() doesn't shoot the alert msg后跟 Response.Redirect() 的警报不会发出警报消息
【发布时间】:2014-05-27 11:22:47
【问题描述】:

我有一条警告消息,然后是 Page_Init 中的 Response.Redirect 块

Response.Redirect 起作用并重定向到特定页面。 但是没有显示警报消息。

我需要用户单击警报消息中的“确定”按钮,之后只有页面应该重定向..

请帮忙。

示例代码:

ScriptManager.RegisterCleintScriptBlock(this,this.GetType(),"alert('Success')",true); Response.Redirect("index.aspx",false);

【问题讨论】:

    标签: javascript asp.net page-init


    【解决方案1】:

    它永远不会工作,因为上下文丢失了。 alert() 应该在index.aspx.cs 中完成。

    这是可能的实现:

    Response.Redirect("index.aspx?query=alert",false);
    

    并且在 index.aspx.cs Page_Load:

    if(Request["query"]=="alert")
    {
        ScriptManager.RegisterClientScriptBlock(this.GetType(),"key","alert('Success')",true); 
    }
    

    【讨论】:

      【解决方案2】:

      为什么不用纯 JavaScript 来做呢?

      <script>
          (function() {
              alert("Success");
              window.location = "/index.aspx";
          })();
      </script>
      

      【讨论】:

        猜你喜欢
        • 2012-08-22
        • 1970-01-01
        • 2013-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多