Jump to content

Back Button Blues


Wylbur
 Share

Recommended Posts

Hi;I'm developing a web app in ASP.NET that loads controlsdynamically on a page (depending upon user responses).Handling the back button has been giving me fits.Apparently, there is no handler for that.(If I'm wrong about that, then PLEASE correct me.)What I want to do is to trigger a postback on the back button.(This must be done. There are no alternatives.)I'm trying to detect the "back button state" to accomplish this.The first thing I tried was to keep a running count ofrequest/response cycles and comparing it to window.history.length.If the two are different, then it might be assumed that the backbutton had been clicked.That didn't work. On the first back button click, both countsincreased, remaining equal.My next idea was to write Javascript code to a label that willset a boolean to true if a postback has occurred.In my Page_Load handler, I have the following: this.lblJavaScript.EnableViewState = false; this.lblJavaScript.InnerText = "postback_made = true;";This is the tag for the element: <span class="CI12_test" id="lblJavaScript" name="lblJavaScript" runat="server"></span>(This tag is positioned above the form and within the body.)In the function that handles the onload event in Javascript,I have the following: var js_code = document.getElementById("lblJavaScript").innerHTML; var ev = window.eval (js_code); document.getElementById("lblJavaScript").innerHTML = "postback_made = false;"The problem that I'm having is that, when the back button is clicked,the "lblJavaScript" element is reset to its original value("postback_made = true;"). I tried moving the code for clearing the switch into a function thathandles the "onbeforeunload(e)" event.That didn't work.(FYI: The "onbeforeunload" event fires every time, whether on a postbackor back button click.)Apparently, the values for the elements within the DOM are being cachedand reloaded on a back button event.Since I'm not quite the biggest idiot in the world, I decided that thenext thing to do would be to shut off caching. I added the following code to my Page_Load event handler: if (!IsPostBack) { Response.Buffer = true; Response.CacheControl = "no-cache"; Response.AddHeader("Pragma", "no-cache"); Response.Expires = -1441; }That does nothing.I added the following code to my Page_Load event handler:Response.Cache.SetCacheability(HttpCacheability.NoCache);Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));Response.Cache.SetNoStore();When I clicked on the back button, I was left with "Webpage has expired".I added the following meta tags near the top of the page:<meta http-equiv="Cache-Control" content="no-cache"/><meta http-equiv="Pragma" content="no-cache"/><meta http-equiv="Expires" content="0"/>That did nothing.So the question is: How would someone determine if the page beingloaded is due to a postback or a back button click?Alternatively, how would someone force a postback on a back button click?Any advice on this would be greatly appreciated.

Link to comment
Share on other sites

 

Hi Dave;

 

I've seen those.

 

The first one applies to server-side functionality.

 

The problem is that, when the back button is pressed,

it does not trigger a postback event, so whatever server-side

code there is won't be executed.

 

The second one looked good at first, but it only applies to

AJAX/Updatepanel/asynchronous postbacks. It threw an exception when

I tried it on a just plain synchronous postback.

 

THANKS for the response!

Link to comment
Share on other sites

Basically, if I could determine when the back and forward buttons were clicked

(with Javascript) I'd probably have what I need to resolve my issue. It would be

nice if there were "onbackbutton" and "onforwardbutton" event handlers, but

I've yet to find anything that would fill those roles. I'm hoping that someone might

give me a clue on something that I've overlooked.

 

I see that there's a "window.onpopstate" event, but it doesn't seem to be

doing anything for me as of yet. I'm going to keep playing with it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...