Asp.net Webform显示警报和重定向

Alt*_*ept 7 javascript asp.net redirect alert

我现在卡住了.我有一个带有按钮的webform,可以注册或保存记录.我想要的是让它显示一个javascript警报,然后重定向到一个页面.这是我正在使用的代码

protected void Save(..)
{   
    // Do save stuff
    DisplayAlert("The changes were saved Successfully");
    Response.Redirect("Default.aspx");
}
Run Code Online (Sandbox Code Playgroud)

此代码只是重定向而不提示提示已成功保存.

这是我的DisplayAlert代码

 protected virtual void DisplayAlert(string message)
    {
        ClientScript.RegisterStartupScript(
                        this.GetType(),
                        Guid.NewGuid().ToString(),
                        string.Format("alert('{0}');", message.Replace("'", @"\'").Replace("\n", "\\n").Replace("\r", "\\r")),
                        true
                    );
    }
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我找到解决方案吗?

谢谢

Kel*_*tex 7

您无法执行Response.Redirect,因为您的javascript警报将永远不会显示.最好让您的javascript代码windows.location.href='default.aspx'在显示警报后实际执行重定向.像这样的东西:

protected virtual void DisplayAlert(string message)
{
    ClientScript.RegisterStartupScript(
      this.GetType(),
      Guid.NewGuid().ToString(),
      string.Format("alert('{0}');window.location.href = 'default.aspx'", 
        message.Replace("'", @"\'").Replace("\n", "\\n").Replace("\r", "\\r")),
        true);
}
Run Code Online (Sandbox Code Playgroud)