2011-12-20 50 views
7

我正在使用vs 2010.我需要向用戶顯示消息並重定向頁面。如何在重定向頁面之前獲取警報消息

我使用下面的行。

ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "<script> alert('User details saved sucessfully');window.open('frmDisplayUsers.aspx');</script>", true); 

但我沒有收到警報消息,並且該頁面被直接重定向。

如何獲取警報消息?

回答

23

你的代碼是開放的窗口,但你要求一個重定向,下面是一個重定向的例子:

ScriptManager.RegisterStartupScript(this, this.GetType(), 
"alert", 
"alert('User details saved sucessfully');window.location ='frmDisplayUsers.aspx';", 
true); 
1

你需要寫:

ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", " alert('User details saved sucessfully'); window.open('frmDisplayUsers.aspx');", true); 

請注意,我已經刪除了腳本標籤的最後一個參數真正意味着你不能使用的腳本。

此代碼適用於我。如果您有任何問題,請告訴我。另外,您可以使用setTimeout延遲打開窗口,這可能不是一個非常糟糕的選擇。

0

的最佳方式,如果可能的話,就是把代碼放到的OnClientClick。

<asp:Button OnClientClick=" alert('blah?');" runat="server" /> 

將它放入startupscript的問題是startupscript在回發上運行,而不是實時。重定向將在回發之前發生。 (可能)

另一個解決方案當然是將重定向放入startupscript代碼中。

該頁面將回發,該腳本將運行,然後它將重定向。

1

如果你希望把.CS文件,只是試試這個:

var page = HttpContext.Current.CurrentHandler as Page; 
      ScriptManager.RegisterStartupScript(page, page.GetType(), "alert", "alert('" + msg +"');window.location ='"+ aspx +"';", true); 
0

如果您正在使用的UpdatePanel的工作,然後你必須使用: 它與更新面板的工作。

var message = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize("Bill No. : " + BillNo + " successfully generated."); 
      var script = string.Format("alert({0});window.location ='ChhallanPrint.aspx';", message); 
      ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", script, true); 
-1

你問一個重定向頁面,下面是一個重定向的例子:

string page = "Login.aspx"; 
     string myStringVariable = "Password Update!"; 
     ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');Response.Redirect('"+ page +"');", true); 
0

重定向到登錄頁面後的密碼更新警報消息

Dim MyScript As String = "alert('Password Updated Successfully!. Please Login Again!'); window.location = '../Login.aspx';" 

如果你的目的是將您的頁面設置爲頂層,因爲您的頁面可能位於框架內的框架內。

Dim MyScript As String = "alert('Password Updated Successfully!. Please Login Again!');window.top.location='../Logout.aspx';" 
      ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "MyScript", MyScript, True) 
相關問題