【问题标题】:Refresh page and open new new window刷新页面并打开新窗口
【发布时间】:2014-11-07 09:31:46
【问题描述】:

我有一个页面在灯箱内显示一些内容,在此内容中有一个按钮 (btnAccept) 来确认。我想从 codeBehind (c#) 刷新页面并打开一个新窗口(或相反)。我将不胜感激。

这是我迄今为止尝试过的:

第一次尝试:我可以打开一个新窗口,但我无法刷新页面

  protected void btnAccept_Click(object sender, EventArgs e)
    {
    //to open the new tab
    Response.Redirect(URL, true); 
    //to refresh the page
    Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);

    }

第二次尝试:我可以打开一个新窗口,但我无法刷新页面

  protected void btnAccept_Click(object sender, EventArgs e)
    {
    //to open the new tab
    ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('URL');", true);

    //to refresh the page
    Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);

    }

如果我更改顺序,我将刷新页面但不会打开新页面

【问题讨论】:

  • 我猜是因为你正在结束响应,否则页面会被刷新。它不是最好的,但我能想到这个解决方案->按下按钮时使用 Response.Redirect(HttpContext.Current.Request.Url.ToString()+"?btnPressed=true");然后在您的页面加载方法上,使用“如果查询字符串是 btnPressed... 等,则 -> ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('URL');",真);
  • 您好 Ziv,谢谢您的回答!很抱歉,但我不太了解您的解决方案,请您再解释一次。提前谢谢!
  • 最好的办法是在代码中,看我的解决方案。

标签: javascript c# code-behind


【解决方案1】:

所以,我的意思是这样做:

 public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btn.Text = "Refreshed";
        if (Request.Params["btnPressed"] != null && Request.Params["btnPressed"] == "true")
        {
            ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('MyPage.aspx');", true);
        }
    }

    protected void btn_Click(object sender, EventArgs e)
    {

        btn.Text = "Not Refreshed";
        lbl.Text = "Not Refreshed";
        System.Threading.Thread.Sleep(1000);
        ////to refresh the page
        Page.Response.Redirect(HttpContext.Current.Request.Url.ToString()+"?btnPressed=true", true);
    }
}

【讨论】:

  • Ziv,谢谢,很好的解决方案!
  • Ziv,请你帮忙解决我的新问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-03
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多