Answered by:
disable the back button from code behind in asp.net

Question
-
User351619809 posted
<div class="post-text" itemprop="text">
I am trying to disable the back button from code behind in asp.net. I already know how to do it in javascript. I have the following code in my page_init method, but I can still see the back button enabled in chrome:
protected void Page_Init(object sender, EventArgs e) { Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1)); Response.Cache.SetNoStore(); }
I want the button to be disabled so that the user cannot click on it.
Wednesday, July 15, 2020 1:43 PM
Answers
-
User475983607 posted
I want the button to be disabled so that the user cannot click on it.It's not possible to disable the back button. You can remove/modify the history using JavaScript though.
https://developer.mozilla.org/en-US/docs/Web/API/History_API
Usually the Post/Redirect/Get pattern solves this issue.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 15, 2020 1:50 PM -
User1535942433 posted
As far as I think,your requirment is impossible.It will not work if the forward page does not exist in the history list.So,disabling browser caching in code behind could prevent users navigate back to another page by using browser back button,however,it couldn't disable the back button.
I suggest you could do this is to pass a token on every URL within the application, and within every form. The token is regenerated on every page, and once the user loads a new page any tokens from previous pages are invalidated.When the user loads a page, the page will only show if the correct token (which was given to all links/forms on the previous page) was passed to it.
Another way is this by server-side tracking of the user session, and redirecting (as in Server.Transfer, not Response.Redirect) the user/browser to the required page.
More details,you could refer to below article:
https://stackoverflow.com/questions/961188/disable-browsers-back-button
https://www.codeproject.com/Questions/732138/disable-back-button-in-asp-net-using-csharp
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 16, 2020 6:19 AM
All replies
-
User475983607 posted
I want the button to be disabled so that the user cannot click on it.It's not possible to disable the back button. You can remove/modify the history using JavaScript though.
https://developer.mozilla.org/en-US/docs/Web/API/History_API
Usually the Post/Redirect/Get pattern solves this issue.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 15, 2020 1:50 PM -
User1535942433 posted
As far as I think,your requirment is impossible.It will not work if the forward page does not exist in the history list.So,disabling browser caching in code behind could prevent users navigate back to another page by using browser back button,however,it couldn't disable the back button.
I suggest you could do this is to pass a token on every URL within the application, and within every form. The token is regenerated on every page, and once the user loads a new page any tokens from previous pages are invalidated.When the user loads a page, the page will only show if the correct token (which was given to all links/forms on the previous page) was passed to it.
Another way is this by server-side tracking of the user session, and redirecting (as in Server.Transfer, not Response.Redirect) the user/browser to the required page.
More details,you could refer to below article:
https://stackoverflow.com/questions/961188/disable-browsers-back-button
https://www.codeproject.com/Questions/732138/disable-back-button-in-asp-net-using-csharp
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 16, 2020 6:19 AM