How do I stop the Back and Refresh buttons from resubmitting my form?

后端 未结 13 1281
时光说笑
时光说笑 2020-11-27 15:16

I am doing web development.

I have a page to do with credit card, which when user click \"refresh\" or \"Back\", the transaction will be performed one more time, wh

13条回答
  •  借酒劲吻你
    2020-11-27 15:32

    Just put this javascript on the html section of aspx page above head section

    
    

    We need to put it on the html section of the page which we want to prevent user to visit by hitting the back button

    Complete code of the page looks like this

    <%@ Page Language="C#" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    
    
    
    
    Untitled Page
    
    
    
    
    This is First page

    Go to Second page

    Go to Second Page

    If you are using firefox then use instead of onload

    If you want to disable back button using code behind of aspx page,than you need to write below mentioned code C# code behind

    protected override void OnPreRender(EventArgs e)
    {
    base.OnPreRender(e);
    string strDisAbleBackButton;
    strDisAbleBackButton = "";
    ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "clientScript", strDisAbleBackButton);
    } 
    

    We can also achieve this by disabling browser caching or cache by writing this line of code either in Page_load event or in Page_Init event

    protected void Page_Init(object Sender, EventArgs e)
    {
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
    Response.Cache.SetNoStore();
    }
    

    Doing this,user will get the page has expired message when hitting back button of browser

    Demo is :

    enter image description here

提交回复
热议问题