How to Prevent Browser Back After Record Insert in ASP.NET

58 Views Asked by At

I'm trying to prevent the previous values shown when the user clicks the browser's back button after a record insert using ASP.NET.

I know this is a classic question asked many times, but I've spent last 2 days reading answers and trying a number of methods and have not found the proper solution.

Here's an example. Let's say you have 2 pages. Page one is an Order Page where you collect a credit card number. After they insert the order, it takes them to page two which is a Confirmation Page showing success. If the user then clicks the browser's back button, it will take them back to the Order Page and show them the credit card number previously entered. I want to prevent that.

Below I show sample script where I tried to use a method that clears the cache but it doesn't work. I also tried 3 other methods of clearing the cache that I show at the end... along with other notes.

OrderPage.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="OrderPage.aspx.vb" Inherits="OrderPage" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server"><title>Order</title></head>
<body>
    <form id="form1" runat="server" class="container">
        <h1>Order Item</h1>
        <asp:FormView ID="FormView1" runat="server" DefaultMode="Insert" DataKeyNames="OrderId" DataSourceID="SqlDataSource1">
            <InsertItemTemplate>
                CreditCard:<br /> 
                <asp:TextBox ID="TextBoxCreditCard" runat="server" Text='<%# Bind("CreditCard") %>' /><br />
                <asp:LinkButton ID="ButtonInsert" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
            </InsertItemTemplate>
        </asp:FormView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:MyString %>" InsertCommand="INSERT INTO [Order] ([CreditCard]) VALUES (@CreditCard)">
            <InsertParameters>
                <asp:Parameter Name="CreditCard" Type="String" />    
            </InsertParameters>
        </asp:SqlDataSource>
    </form>
</body>
</html>

Partial Class OrderPage
    Inherits System.Web.UI.Page

    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Response.Buffer = True
        Response.ExpiresAbsolute = Now()
        Response.Expires = -1
        Response.CacheControl = "no-cache"

        If Not Page.IsPostBack = True Then
        End If
    End Sub

    Private Sub FormView1_ItemInserted(sender As Object, e As FormViewInsertedEventArgs) Handles FormView1.ItemInserted
        Response.Redirect("ConfirmPage.aspx?m-1")
    End Sub
End Class

ConfirmPage.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ConfirmPage.aspx.vb" Inherits="ConfirmPage" %>
<!doctype html>
<html lang="en">
<head runat="server"><title>Confirmed</title></head>
<body>
    <form id="form1" runat="server">
        <h1>Success</h1>
    </form>
</body>
</html>

Partial Class ConfirmPage
    Inherits System.Web.UI.Page

    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Call PreventBackButtonMethods(MyMethod:=2)
    End Sub
End Class

Some notes:

  • I understand you cannot disable the back button. That is not what I'm trying to do. I just don't want them to be able to navigate back to previously entered values.
  • I understand this can be done with Javascript, but I'd prefer ASP.NET in case Javascript is disabled on the client side (and the JS method I found has a screen flicker). However, if JS is the only way, I'd appreciate knowing that so I stop spinning.
  • I tried using a Session variable, but it didn't work because pressing the browswer's back button doesn't cause a Page event (eg. load, preint, etc..) that I can add code to, to use that session variable (unless I did something wrong).

Other clearing cache methods I've tried:

Method 1: Response.Buffer = True Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1) Response.Expires = 0 Response.CacheControl = "no-cache"

Method 2: Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1)) Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.Cache.SetNoStore()

Method 3: Response.Cache.SetNoStore() Response.Cache.AppendCacheExtension("no-cache") Response.Expires = 0

0

There are 0 best solutions below