c# -页面重定向不能正常工作

本文关键字:工作 常工作 -页 重定向 不能 | 更新日期: 2023-09-27 18:10:05

我有以下c#代码:

if (function.Equals("Larger50"))
{
      Request req = new Request();
      string result = req.doRequest("function=" + function + "&num=" + number, "http://localhost:4000/Handler.ashx");
      if (result.Equals("True") || result.Equals("true"))
      {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Larger.aspx?num=" + number + "', '_newtab')", true);
       }
       if(result.Equals("False") || result.Equals("false"))
       {
             Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Smaller.aspx', '_newtab')", true);
       }
       if(result.Equals("Error") || result.Equals("error"))
       {
             Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/ErrorPage.htm', '_newtab')", true);
       }
       Session["result"] = result;
       Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.location.href = 'Results.aspx'", true);
}

结果变量可以是以下三个值中的任意一个(如果服务器响应):

我真的(二)假3)误差

这段代码的主要问题是三个if语句中的新制表符脚本都按其应有的方式工作。但是,打开Results的最后一个脚本。Aspx页由于某种原因没有执行。这个脚本写得很好,因为如果所有其他代码都被注释掉,它会完美地执行。我该如何解决这个问题?

我试着用Response.Redirect("Results.aspx")替换它,然而,这执行和所有其他三个脚本从未执行。

c# -页面重定向不能正常工作

您应该一次注册所有这些,而不是在两个单独的语句中:

if (function.Equals("Larger50"))
{
      Request req = new Request();
      string result = req.doRequest("function=" + function + "&num=" + number, "http://localhost:4000/Handler.ashx");
      string scriptVal = "";
      if (result.Equals("True") || result.Equals("true"))
      {
            scriptVal = "window.open('http://localhost:4000/Larger.aspx?num=" + number + "', '_newtab');";
       }
       if(result.Equals("False") || result.Equals("false"))
       {
            scriptVal = "window.open('http://localhost:4000/Smaller.aspx', '_newtab');";
       }
       if(result.Equals("Error") || result.Equals("error"))
       {
            scriptVal = "window.open('http://localhost:4000/ErrorPage.htm', '_newtab');";
       }
       Session["result"] = result;
       scriptVal += "window.location.href = 'Results.aspx';";
       Page.ClientScript.RegisterStartupScript(Page.GetType(), null, scriptVal, true);
}

参见ClientScriptManager.RegisterStartupScript的文档,具体如下:

启动脚本由其键和类型唯一标识。具有相同键和类型的脚本被认为是重复的。只有一个具有给定类型和密钥对的脚本可以在页面中注册。尝试注册已注册的脚本不会创建一个脚本的副本。

在您的示例中,类型和键在您注册的两个脚本中是相同的。

您可以用密钥唯一地标识它们,然后分别注册它们。但是您必须记住,执行顺序不能保证:

脚本块不能保证按它们的顺序输出注册。