Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi;

I'm developing a web app in ASP.NET that loads controls
dynamically 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 of
request/response cycles and comparing it to window.history.length.
If the two are different, then it might be assumed that the back
button had been clicked.

That didn't work. On the first back button click, both counts
increased, remaining equal.

My next idea was to write Javascript code to a label that will
set 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"

(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 that
handles the "onbeforeunload(e)" event.

That didn't work.

(FYI: The "onbeforeunload" event fires every time, whether on a postback
or back button click.)

Apparently, the values for the elements within the DOM are being cached
and reloaded on a back button event.

Since I'm not quite the biggest idiot in the world, I decided that the
next 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 being
loaded 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.
Posted
Comments
[no name] 29-Sep-14 5:28am    
please reformat what you have posted.
thanks

1 solution

Back button Handling[^]

Please check this if, this is what you want.
Thanks
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900