简体   繁体   中英

Asp.Net Disable Back button

I have a 3 pages from 1st page to second and second to third im sending some information.what i wanted to do is if im in the second page i want user to not to go back to 1st page again or in 3rd place users can go to 1st or 2nd page.

Is there is a way to disable back button on browser?or is there is a way to show an error when user press back button?

Any suggestions?

Try below code for disable browser back button using javascript.

<script type="text/javascript" language="javascript">
function DisableBackButton() {
  window.history.forward()
}
DisableBackButton();
window.onload = DisableBackButton;
window.onpageshow = function(evt) { if (evt.persisted) DisableBackButton() }
window.onunload = function() { void (0) }
</script>

This is my old code for that inside my Master Page It's been a couple of years since I made this, your choice if you want to update it.

    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
       MyBase.OnPreRender(e)
       Dim disAbleBackButton As String
       disAbleBackButton = "<script language='javascript'>" & Environment.NewLine
       disAbleBackButton &= "function noBack(){window.history.forward()}" & Environment.NewLine
       disAbleBackButton &= "noBack();" & Environment.NewLine
       disAbleBackButton &= "window.onload=noBack;" & Environment.NewLine
       disAbleBackButton &= "window.onpageshow=function(evt){if(evt.persisted)noBack()}" & Environment.NewLine
       disAbleBackButton &= "window.onunload=function(){void(0)}" & Environment.NewLine
       disAbleBackButton &= "</script>"
       Page.ClientScript.RegisterClientScriptBlock(Me.Page.GetType(), "backhistory", disAbleBackButton)
  End Sub

Try below code to block back & forward buttons on the browser.

window.history.pushState(null, "", window.location.href);  
window.history.back();
window.history.forward();      
window.onpopstate = function() {
    window.history.pushState(null, "", window.location.href);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM