ASP.NET WebForms: page data not cleared after redirect when returning with browser back

1 day ago 1
ARTICLE AD BOX

Here's a few different options to handle this with JavaScript:

You can detect if the page was navigated to from the browser history like in this answer.

Another option would be to show a panel (or if you have nothing to execute on the server side, just a button) with JavaScript telling it to redirect and remove the current page from navigation history: window.location.replace('your-link');

This code below will work for detecting if the page shown is loaded from cache instead of from the server, for instance if it was from forward/backwards navigation since that's what I needed in my application.

document.getElementsByTagName("BODY")[0].onpageshow = function() { if (event.persisted) { alert('Please refresh'); //your code here for what to do if page gets displayed from cache } };

The onpageshow event happens whenever the page is displayed, so it works just fine with navigation. event.persisted is a read-only property indicating if the webpage is loading from a cache. This ended up as the perfect solution for me when I was looking to do something similar.

Why your code isn't clearing the fields

When a method redirects, the only thing it tells the browser to do is redirect. It cannot pass the information required to tell your page to reset.

Brandon's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article