Jude Nanayakkara
Jude Nanayakkara

Reputation: 281

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?

Upvotes: 0

Views: 1784

Answers (3)

smdhkv
smdhkv

Reputation: 187

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);
};

Upvotes: 0

rjps12
rjps12

Reputation: 605

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

Upvotes: 1

Kaushik Maheta
Kaushik Maheta

Reputation: 1861

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>

Upvotes: 0

Related Questions