【问题标题】:C# - Programmatically Opening a Pop-Up Window in BrowserC# - 以编程方式在浏览器中打开弹出窗口
【发布时间】:2013-04-18 14:49:47
【问题描述】:

我有以下代码:

if (function.Equals("PopUp"))
            {
                Request req = new Request();
                string result = req.doRequest("function=" + function + "&num=" + trans_number, "http://localhost:4000/Handler.ashx");

                if (result.Equals("True") || result.Equals("true"))
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Transaction_Number.aspx', '_newtab')", true);
                }

                Session["result"] = result;
                Response.Redirect("Results.aspx");
            }

此代码向服务器发出请求,如果结果为真,它应该创建一个新选项卡并将当前窗口重定向到 Results.aspx。

如果结果为假,它应该只重定向到 Results.aspx。

这段代码的主要问题是永远不会创建新选项卡,即使结果为真。但是,如果我注释掉除新选项卡代码之外的所有代码,则会创建新选项卡。

为什么会这样?我该如何纠正?

【问题讨论】:

    标签: c# tabs popup window


    【解决方案1】:

    问题似乎是您在脚本可以执行之前进行了重定向。我也会尝试在脚本中进行重定向,所以是这样的;

    if (function.Equals("PopUp"))
    {
        Request req = new Request();
        string result = req.doRequest("function=" + function + "&num=" + trans_number, "http://localhost:4000/Handler.ashx");
    
        if (result.Equals("True") || result.Equals("true"))
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Transaction_Number.aspx', '_newtab')", true);
        }
    
        Session["result"] = result;
    
        Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.location.href = 'http://localhost:4000/Redirect.aspx.aspx'", true);
    }
    

    【讨论】:

      【解决方案2】:

      结果与您正在测试的结果不匹配肯定存在问题。

      String.Equals() 可能并不总是与给定的字符串匹配,因为有时如果字符串已被保留,则引用可能不匹配。

      我建议切换到使用 String.Compare() 或更好,但只使用相等运算符:==

      所以:

      if (result == "True" || result == "true")
      {
          Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Transaction_Number.aspx', '_newtab')", true);
      }
      

      或者更好:

      if (Convert.ToBoolean(result))
      {
          Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Transaction_Number.aspx', '_newtab')", true);
      }
      

      MSDN 有一些关于如何有效比较字符串的具体指南:

      http://msdn.microsoft.com/en-gb/library/vstudio/cc165449.aspx

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多