2013-07-08 28 views
1

我試圖在response.redirect發生之前顯示一個確認彈出框。當我使用下面的代碼時,我從來沒有得到一個它只是重定向的彈出窗口。任何想法我失蹤?消息框不能與頁面重定向

Try 
     Dim msg As String = "Hello!" 
     Dim script As String = "alert('" & msg & "');" 
     script += "window.location.href ='frmSummary.aspx'" 
     'Navigate to Page 2 
     'Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "SCRIPT", script,true); 
     System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "Test",  script, True) 

    Catch ex As Exception 

    End Try 



    Response.Redirect("~/frmMain.aspx") 
+1

你的VB代碼在服務器上執行。在任何代碼被髮送到客戶端之前,重定向發生。 –

回答

1

我將冒險猜測它是指向frmMain.aspx ..?

您正試圖在客戶端呈現你的Javascript,雖然頁面獲取呈現之前,你與該行執行重定向 - Response.Redirect("~/frmMain.aspx")

儘量評論重定向並再次嘗試。

+0

是的,它在我註釋掉重定向時起作用。有沒有一種方法可以讓用戶在消息框中單擊確定後重定向? – user1342164

+0

點擊後是不是重定向到'frmSummary.aspx'? – Darren

+0

我需要它在ok點擊後重新定位到frmMain – user1342164

2

window.location.href邏輯成JavaScript confirm而不是alert調用,就像這樣:

if (confirm("Do you want to do something?")) { 
    // User clicked OK, so redirect 
    window.location.href = 'frmSummary.aspx'; 
} 
+0

如何捕獲JavaScript中的確定點擊?我從來沒有這樣做,謝謝 – user1342164

+0

對不起,我意識到你需要使用'確認',而不是'警報'。我更新了我的答案。 'alert'不會返回一個值,但'confirm'確實。 –

+0

謝謝!幫助我們很多 – user1342164

2

在您當前的代碼中,服務器在重新渲染到客戶端之前重定向到frmMain.aspx。

如果你想確定,顯示東西回用戶/取消按鈕,你想是這樣的 -

protected void Button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     string script = "if(confirm('Are you sure ...?')) {window.location.href ='frmSummary.aspx'; }"; 
     ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Test", script, true); 
    } 
    catch (Exception ex) 
    { 
    } 
}