响应.重定向消除了脚本管理器操作

Response.Redirect eliminates the script manager action

本文关键字:脚本 管理器 操作 重定向 响应      更新时间:2023-09-26

我有以下问题。我有一个输入客户数据的表单。之后,表单将向用户提供消息,表明操作已成功完成:

ScriptManager.RegisterStartupScript(this, GetType(), "Success", 
                                    "alert('Successfully input');", true);

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

Response.Redirect("customer_data.aspx");

我的问题是,第一个命令不工作,如果我把第二个。如果去掉第二个,它就能正常工作。我甚至试过使用:

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

,但是第一个命令仍然不起作用。请帮助。

这是因为ASP。. NET使您无法理解客户端

上发生的事情。
Response.Redirect("customer_data.aspx");

发送http报头

Location=customer_data.aspx

ScriptManager.RegisterStartupScript(this, GetType(), "Success", 
                                "alert('Successfully input');", true);

呈现
<script type="text/javascript">
  alert('Successfully input');
</script>

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

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

我认为同一页面的Response.Redirect被检测为引起循环。您可以尝试使用script重新加载:

ScriptManager.RegisterStartupScript(this, GetType(), "Success", 
    "alert('Successfully input');window.location.reload(true);", true);

你可以这样写:

string answer = "Success";    
ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect",
            "alert('" + answer + "'); window.location.href='index.aspx';", true);