【问题标题】:Javascript Alert before redirecting in ASP.NET在 ASP.NET 中重定向之前的 Javascript 警报
【发布时间】:2012-08-25 22:41:40
【问题描述】:

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

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

效果很好。

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

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

【问题讨论】:

    标签: c# javascript asp.net ajax updatepanel


    【解决方案1】:

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

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

    【讨论】:

    • 此解决方案也适用于使用路由名称而不是完整页面名称。
    【解决方案2】:

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

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

    window.location = "NextPage.asps";
    

    【讨论】:

    • 如何将它添加到内置函数中?
    【解决方案3】:

    这很好用

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多