2014-11-06 170 views
0

我有一個頁面顯示燈箱內的一些內容,在這個內容中有一個按鈕(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); 

    } 

如果我更改順序,我將刷新頁面,但不會打開新的一頁

+0

我想這是因爲你結束的響應,否則該網頁將被刷新。 它不是最好的,但我可以想到這個解決方案 - >當按下按鈕時,您使用Response.Redirect(HttpContext.Current.Request.Url.ToString()+「?btnPressed = true」); 然後在頁面加載方法中,使用「if query string is btnPressed ... ect,then - > ScriptManager.RegisterStartupScript(Page,typeof(Page),」OpenWindow「,」window.open('URL') ;「,true); – 2014-11-07 00:02:27

+0

Hi Ziv,謝謝你的回答!很抱歉,我沒有完全遵循你的解決方案請你再解釋一次。先謝謝了! – Carlos 2014-11-07 00:13:22

+0

最好的方法是在代碼中,看到我的解決方案 – 2014-11-07 01:31:12

回答

0

所以,我的意思是這樣:

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); 
    } 
} 
+0

Ziv,謝謝你,很好的解決方案! – Carlos 2014-11-07 15:54:21

+0

Ziv,請你幫我解答我的新問題 – Carlos 2014-11-07 16:06:06