Found the fix for it. Here is what you do...
I have a class to hold my scripts here are the subroutines
Public Shared Sub PromptOnNavAway(ByVal Page As System.Web.UI.Page)
Dim script As StringDim sb As New StringBuilder()
sb.Append("<SCRIPT LANGUAGE=JavaScript> " & Environment.NewLine)
sb.Append(
"" & ControlChars.Tab & "function onBeforeUnloadAction()" & Environment.NewLine)sb.Append("" & ControlChars.Tab & "{" & Environment.NewLine)
sb.Append(
"" & ControlChars.Tab & "" & ControlChars.Tab & "" & ControlChars.Tab & "needToConfirm = true;" & Environment.NewLine)sb.Append(" return 'Think twice before you leave!'; " & Environment.NewLine)
sb.Append(
" }" & Environment.NewLine)sb.Append(" " & Environment.NewLine)
sb.Append(
" var needToConfirm = true; " & Environment.NewLine)sb.Append(" " & Environment.NewLine)
sb.Append(
" " & ControlChars.Tab & "window.onbeforeunload = function()" & Environment.NewLine)sb.Append(" " & ControlChars.Tab & "{ " & Environment.NewLine)
sb.Append(
"" & ControlChars.Tab & "" & ControlChars.Tab & "if (needToConfirm)" & Environment.NewLine)sb.Append("" & ControlChars.Tab & "" & ControlChars.Tab & "{ " & Environment.NewLine)
sb.Append(
" " & ControlChars.Tab & "" & ControlChars.Tab & "" & ControlChars.Tab & "if(((window.event.clientX<0) || (window.event.clientY<0)) && (theForm.__EVENTTARGET.value != null))" & Environment.NewLine)sb.Append("" & ControlChars.Tab & "" & ControlChars.Tab & "" & ControlChars.Tab & "{ " & Environment.NewLine)
sb.Append(
" " & ControlChars.Tab & "" & ControlChars.Tab & "" & ControlChars.Tab & "return onBeforeUnloadAction(); " & Environment.NewLine)sb.Append(" " & ControlChars.Tab & "" & ControlChars.Tab & "" & ControlChars.Tab & "} " & Environment.NewLine)
sb.Append(
" " & ControlChars.Tab & "} " & Environment.NewLine)sb.Append("" & ControlChars.Tab & "" & ControlChars.Tab & "else " & Environment.NewLine)
sb.Append(
"" & ControlChars.Tab & "" & ControlChars.Tab & "{ " & Environment.NewLine)sb.Append("" & ControlChars.Tab & "" & ControlChars.Tab & "" & ControlChars.Tab & " return alert('In the Else');" & Environment.NewLine)
sb.Append(
"" & ControlChars.Tab & "" & ControlChars.Tab & "}" & Environment.NewLine)sb.Append(" " & ControlChars.Tab & "} " & Environment.NewLine)
sb.Append(
" " & Environment.NewLine)sb.Append(" " & Environment.NewLine)sb.Append("</SCRIPT>")
script = sb.ToString
Page.ClientScript.RegisterClientScriptBlock(GetType(String), "promptclose", script)
End Sub
Public Shared Sub PromptOnNavRemove(ByVal Page As System.Web.UI.Page)
Dim script As StringDim sb As New StringBuilder()
sb.Append(
"<SCRIPT LANGUAGE=JavaScript> window.onbeforeunload = null; " & Environment.NewLine)sb.Append("</SCRIPT>")
script = sb.ToString
Page.ClientScript.RegisterStartupScript(GetType(String), "promptclose", script)
End SubPublic Shared Sub AddPromptAwayOnEdit(ByVal fv As FormView, ByRef SessionManager_Mode As String, ByRef Page As System.Web.UI.Page)
If ((fv.CurrentMode = FormViewMode.Edit) Or (fv.CurrentMode = FormViewMode.Insert)) Then
SessionManager_Mode =
"Edit"
clsCommon.PromptOnNavAway(Page)
Else
SessionManager_Mode =
""
clsCommon.PromptOnNavRemove(Page)
End If
End SubPublic Shared Sub AddPromptAwayOnPageLoad(ByRef SessionManager_Mode As String, ByRef Page As System.Web.UI.Page)
If SessionManager_Mode = "Edit" Then
clsCommon.PromptOnNavAway(Page)
End If
End Sub
in my webform in the page load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
'Do some stuff if needed
End If
'Add this line
clsCommon.AddPromptAwayOnPageLoad(SessionManager.Mode, Me.Page)
End Sub
'In the formview mode changed event add this line.
Protected Sub fv_ModeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles fvCompanyStatus.ModeChanged
clsCommon.AddPromptAwayOnEdit(fv, SessionManager.Mode,
Me.Page)
End Sub
This will allow you to capture the back and close on a formview that has a autopostback = true control inside of it. The formview is nested inside a masterpage then a sub master page.