Response.Redirect消除了脚本管理器操作

use*_*335 2 javascript c# asp.net

我有以下问题.我有一个表单来输入客户数据.之后,表单将向用户发送消息,指示已成功完成:

ScriptManager.RegisterStartupScript(this, GetType(), "Success", 
                                    "alert('Successfully input');", true);
Run Code Online (Sandbox Code Playgroud)

然后,它将使用以下内容重新加载页面:

Response.Redirect("customer_data.aspx");
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,如果我放第二个命令,第一个命令就不起作用了.如果删除第二个,它工作正常.我甚至尝试过使用:

try
{
    ScriptManager.RegisterStartupScript(this, GetType(), "Success",                                         "alert('Successfully input');", true);
}
finally
{
    Response.Redirect("customer_data.aspx");
}
Run Code Online (Sandbox Code Playgroud)

但是第一个命令再次起作用.请帮忙.

Ond*_*dar 6

这是因为ASP.NET使您无法理解客户端上发生的事情

Response.Redirect("customer_data.aspx");
Run Code Online (Sandbox Code Playgroud)

发送http标头

Location=customer_data.aspx
Run Code Online (Sandbox Code Playgroud)

ScriptManager.RegisterStartupScript(this, GetType(), "Success", 
                                "alert('Successfully input');", true);
Run Code Online (Sandbox Code Playgroud)

呈现

<script type="text/javascript">
  alert('Successfully input');
</script>
Run Code Online (Sandbox Code Playgroud)

在页面的末尾.当然,如果在http标头中设置了位置,则浏览器会执行重定向,并且不会执行页面上的脚本.您可以在显示警报后在javascript中执行重定向:

ScriptManager.RegisterStartupScript(this, GetType(), "Success", 
  "alert('Successfully input');location.href='customer_data.aspx'", true);
Run Code Online (Sandbox Code Playgroud)