locked
check for use of browser back button to prevent re-triggering of event RRS feed

  • Question

  • User-1037683814 posted

    I have a vb.net form with a save button that saves some stuff to the db. There is a button to view a printer friendly version of the form that, before it redirects to the printer friendly page, it saves again to make sure the user doesn't leave without submitting any changes. If the user  browses to a printer friendly page for the form, then uses the browser's back button to return to the page, the save function is triggered again. I have looked to check for IsPostBack, but it seems to be true both with the click of the save button, and with the browse back. Is there a good way to catch this to prevent the page from triggering this event again?

    Saturday, April 13, 2019 4:35 PM

All replies

  • User475983607 posted

    I have a vb.net form with a save button that saves some stuff to the db. There is a button to view a printer friendly version of the form that, before it redirects to the printer friendly page, it saves again to make sure the user doesn't leave without submitting any changes. If the user  browses to a printer friendly page for the form, then uses the browser's back button to return to the page, the save function is triggered again. I have looked to check for IsPostBack, but it seems to be true both with the click of the save button, and with the browse back. Is there a good way to catch this to prevent the page from triggering this event again?

    Use the Post/Redirect/Get pattern.

    https://en.wikipedia.org/wiki/Post/Redirect/Get

    Saturday, April 13, 2019 7:08 PM
  • User-1174608757 posted

    Hi Angate,

    According to your description, I have made a sample here.You could add Response.redirect in the below of all the codes. It will redirect to your page and when you refresh the page , it won't post the data once again. Here is a demo , I hope it could help you.

    aspx:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
              <div>
                
                <asp:GridView ID="GridView1" runat="server"></asp:GridView>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
    </html>
    

    aspx.cs:

    Public Class Test2
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Dim sql As String = "select * from students"
                GridView1.DataSource = sqlhelper.ExecuteDataTable(sql)
                GridView1.DataBind()
            End If
        End Sub
    
    
    
        Protected Sub Button1_Click(sender As Object, e As EventArgs)
            Dim sql As String = "insert into students values('c1','cc')"
            sqlhelper.ExecuteNonQuery(sql)
            Response.Redirect("Test.aspx")
        End Sub
    
    End Class

    It works well as below:

    Best Regards

    Wei

    Monday, April 15, 2019 6:09 AM