2014-02-25 28 views
1

我想阻止客戶端離開頁面在我的asp.net應用程序 沒有保存,下面的代碼可以通過點擊'確定' 確認離開頁面不保存。 覆蓋確認功能在我的瀏覽器(IE8)不工作取消卸載窗口JavaScript沒有確認

<script type="text/javascript"> 
    IsSaved = false; 
    window.onbeforeunload = function (e) { 
        return IsSaved ;   } 
</script> 

,其中客戶端保存數據:

IsSaved = true; 

編輯: 我想禁用點擊「確定」按鈕了。 謝謝。

+0

我不知道你的問題是什麼... – epascarello

+0

我希望在確認時點擊「確定」,禁用離開頁面而不保存 – mich

+0

這是不可能的 – epascarello

回答

4

您不能覆蓋確認功能,因爲它是確認功能。如果瀏覽器不會因爲頁面不想關閉而關閉頁面,那將是一個可怕的安全漏洞。

像其他人一樣確認。並自動保存。

window.onbeforeunload = function() { 
    return !isSaved && "You have unsaved (stuff). Are you sure you want to quit?"; 
}; 
+0

我希望p revent點擊確定也確認,禁用離開頁面不保存 – mich

+1

@mich就像他說的那樣,這是不可能的。 – mason

0

我創建了一個庫,可以很容易地包含在我的所有網站中,以便輕鬆訪問此功能。

將這個類放入一個新的C#類庫項目中,以便您可以從其他項目(網站)中引用它。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace MyNameSpace.ConfirmLeavePageScript 
    { 
    [DefaultProperty("TextToDisplay")] 
    [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")] 
    public class ConfirmLeavePageScript : WebControl 
     { 
     [Bindable(true)] 
     [Category("Appearance")] 
     [DefaultValue("Are you sure you want to leave this page?")] 
     [Localizable(true)] 
     public string TextToDisplay 
      { 
      get 
       { 
       String s = (String)ViewState["TextToDisplay"]; 
       return ((s == null) ? "Are you sure you want to leave this page?" : s); 
       } 

      set 
       { 
       ViewState["TextToDisplay"] = value; 
       } 
      } 

     protected override void RenderContents(HtmlTextWriter output) 
      { 
      string script = @" 

      <script type='text/javascript'> 
    var confirmOnPageExit = function (e) 
    { 
     // If we haven't been passed the event get the window.event 
     e = e || window.event; 

     var message = '" + TextToDisplay + @"'; 
     if(typeof ConfirmLeavePageMessage != 'undefined') //give client side the opportunity to overwrite the message instead of using message from ViewState. 
      { 
      message=ConfirmLeavePageMessage; 
      } 

     // For IE6-8 and Firefox prior to version 4 
     if (e) 
     { 
      e.returnValue = message; 
     } 

     // For Chrome, Safari, IE8+ and Opera 12+ 
     return message; 
    }; 

    function EnableConfirmOnPageExit() 
     { 
     // Turn it on - assign the function that returns the string 
     window.onbeforeunload = confirmOnPageExit; 
     } 


function DisableConfirmOnPageExit() 
     { 
    // Turn it off - remove the function entirely 
    window.onbeforeunload = null; 
    } 
</script> 

      "; 
      output.Write(script); 
      } 
     } 
    } 

您可以在您的web站點項目中註冊標記前綴,方法是將其放置在web.config中。確保你的網站添加了C#類庫項目作爲參考(把它們放在同一個解決方案中)。

<configuration> 
    <system.web> 
     <pages> 
      <controls> 
       <add tagPrefix="tna" assembly="ConfirmLeavePage" namespace="MyNameSpace.ConfirmLeavePageScript" /> 
      </controls> 
     </pages> 
    </system.web> 
</configuration> 

然後,將控件放在內容頁面的頭部。

<head> 
<tna:ConfirmLeavePageScript runat="server" TextToDisplay="Are you sure you want to leave without saving?" /> 
</head> 

在這種檢測是否已經作了修改,調用這個函數JavaScript函數:

EnableConfirmOnPageExit(); 

你也可以做以下(我認爲是自我解釋):

<script type="text/javascript> 
ConfirmLeavePageMessage="new message"; //set a custom message from JavaScript for the confirmation window 
DisableConfirmOnPageExit(); //Disables the confirmation window (user can leave page without seeing the confirmation window) 
</script> 
+1

3行JavaScript代碼的所有代碼? – epascarello

+0

不確定你的意思是「所有代碼」。 JavaScript功能支持新舊瀏覽器。它還使得可以輕鬆地從服務器端修改顯示的消息,並使其可以輕鬆地在多個項目之間移植。 – mason