Detect back button not able to by pass confirm on autopostback

Last post 08-20-2007 11:14 AM by spafa9. 5 replies.

Sort Posts:

  • Detect back button not able to by pass confirm on autopostback

    08-14-2007, 4:04 PM
    • Loading...
    • spafa9
    • Joined on 08-18-2004, 5:15 AM
    • Posts 87

    This code works and confirms like it is supposed to. I am also able to remove it when the user gets out of edit mode on a form view.  My issue now is on my form view I have a dropdown that has an autopostback.  This form view is on a page that is display through a submaster page. It seems to have the same event code as a close or a back button press and will confirm everytime the user selects a new value.  I tried adding in needtoconfirm boolean value and setting it to false on the bind of the formview in insert mode but it totally removes is and dont know how to reset it back to confirm if the user stays in edit mode.    I hope this makes since if not please reply and I will try and make it more clear. Here are the methods I created below..

     

     

    Public Shared Sub PromptOnNavAway(ByVal Page As System.Web.UI.Page)

    Dim script As String

    Dim sb As New StringBuilder()

     

     

    sb.Append(
    "<SCRIPT LANGUAGE=JavaScript> " & Environment.NewLine)

    sb.Append("function onBeforeUnloadAction(){" & Environment.NewLine)

    sb.Append(" return ""Think twice before you leave!"";" & Environment.NewLine)

    sb.Append(" }" & Environment.NewLine)

    sb.Append(" var needToConfirm = true;" & Environment.NewLine)

    sb.Append("" & Environment.NewLine)

    sb.Append(" window.onbeforeunload = function(){" & Environment.NewLine)

    sb.Append("" & Environment.NewLine)

    sb.Append(" if (needToConfirm){" & Environment.NewLine)

    sb.Append(" if((window.event.clientX<0) || (window.event.clientY<0)){ " & Environment.NewLine)

    sb.Append(" return onBeforeUnloadAction();" & Environment.NewLine)

    sb.Append(" }" & Environment.NewLine)

    sb.Append(" }" & Environment.NewLine)

    sb.Append(" } " & Environment.NewLine)

    sb.Append("</SCRIPT>")

     

    script = sb.ToString

    Page.ClientScript.RegisterStartupScript(
    GetType(String), "promptclose", script)

    End Sub

    Public Shared Sub BypassModifiedMethod(ByVal wc As WebControl, ByVal bypass As Boolean)

    wc.Attributes("onclick") = "javascript: needToConfirm = " & bypass & ";"

    End Sub

     

     

    Public Shared Sub PromptOnNavRemove(ByVal Page As System.Web.UI.Page)

    Dim script As String

    Dim 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 Sub

    -------------
    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Detect back button not able to by pass confirm on autopostback

    08-15-2007, 8:44 AM
    • Loading...
    • spafa9
    • Joined on 08-18-2004, 5:15 AM
    • Posts 87

    I have found that it only happens on  a control that has a postback in a formview.  Now I need to figure out how to bypass this control on the postback so it doesnt confirm leaving the page.

     

    -------------
    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Detect back button not able to by pass confirm on autopostback

    08-15-2007, 2:28 PM
    • Loading...
    • spafa9
    • Joined on 08-18-2004, 5:15 AM
    • Posts 87

    I found a way to remove the script but after the postback I need to know how to add it back on. uhhh.

    -------------
    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Detect back button not able to by pass confirm on autopostback

    08-16-2007, 1:54 PM
    • Loading...
    • spafa9
    • Joined on 08-18-2004, 5:15 AM
    • Posts 87

    ok I have it where it will confirm on the x and back button but now I have noticied the formviews that have controls that postback like my dropdown that it will confirm on that also because it thinks the user is navigating away from the page.  I need to know if there is a way of knowing that it is a postback vs a back button or close?  I have been trying to get the _EVENTTARGET value and if it is null then add the script else dont but since I have a masterpage then a sub master page I am having issues in my javascript to get the page params to get the _EVENTTARGET value.  Any ideas would be greatly appreciated !!! Thanks!Big Smile

    -------------
    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Detect back button not able to by pass confirm on autopostback

    08-20-2007, 11:14 AM
    • Loading...
    • spafa9
    • Joined on 08-18-2004, 5:15 AM
    • Posts 87

    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 String

    Dim 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 String

    Dim 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 Sub

    Public 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 Sub

    Public 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.

     

    -------------
    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Detect back button not able to by pass confirm on autopostback

    08-20-2007, 11:14 AM
    Answer
    • Loading...
    • spafa9
    • Joined on 08-18-2004, 5:15 AM
    • Posts 87

    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 String

    Dim 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 String

    Dim 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 Sub

    Public 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 Sub

    Public 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.

     

    -------------
    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
Page 1 of 1 (6 items)
Microsoft Communities
Page view counter