单击后退按钮时触发postbak [英] Trigger a postbak when back button clicked

查看:55
本文介绍了单击后退按钮时触发postbak的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨;



我在ASP.NET中开发一个Web应用程序,在页面上动态加载控件

(取决于用户回复)。



处理后退按钮一直给我配合。



显然,有没有处理程序。

(如果我错了,那么请纠正我。)



我想做的是在后退按钮上触发回发。

(必须这样做。没有其他选择。)



我正在尝试检测后退按钮状态以实现此目的。



我尝试的第一件事是保持运行计数

请求/响应循环并将其与window.history.length进行比较。

如果两者不同,则可能会假设已单击后面的

按钮。



这不起作用。在第一个后退按钮点击,两个计数

增加,保持相等。



我的下一个想法是将Javascript代码写入标签将

如果发生回发,则将布尔值设置为true。



在我的Page_Load处理程序中,我有以下内容:



this.lblJavaScript.EnableViewState = false;



this.lblJavaScript.InnerText =postback_made = true;;



这是元素的标签:



span class =CI12_testid =lblJavaScript name =lblJavaScriptrunat =server



(此标签位于表格上方和正文内。)



在Javascript中处理onload事件的函数中,

我有以下内容:



var js_code = document .getElementById(lblJavaScript)。innerHTML;



var ev = window.eval(js_code);



document.getElementById(lblJavaScript)。innerHTML

=postback_made = false;



问题我所拥有的是,当单击后退按钮时,

lblJavaScript元素被重置为其原始值

(postback_made = true;) 。



我尝试将用于清除开关的代码移动到

处理onbeforeunload(e)事件的函数中。 />


这不起作用。



(仅供参考:onbeforeunload事件每次都会触发,无论是回发

或后退按钮点击。)



显然,DOM中元素的值正在被缓存

并重新加载后退按钮事件。



因为我不是世界上最大的白痴,所以我决定了

接下来要做的就是关闭缓存。



我将以下代码添加到我的Page_Load事件处理程序:



if(!IsPostBack)

{

Response.Buffer = true;

Response.CacheControl =no-cache;

Response.AddHeader (Pragma,no-cache);

Response.Expires = -1441;

}



什么都不做。



我将以下代码添加到我的Page_Load事件处理程序:



回复.Cache.SetCacheability(HttpCacheability.NoCache);

Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));

Response.Cache.SetNoStore() ;



当我点击后退按钮时,我的网页已经过期了。





我在页面顶部附近添加了以下元标记:



< meta http-equiv =Cache-Control content =no-cache/>

< meta http-equiv =Pragmacontent =no-cache/>

< meta http-equiv =Expirescontent =0/>



没有做任何事情。



所以问题是:如何确定加载的页面是否为b $ b单击后退或后退按钮?



或者,有人会如何强制在后退按钮上点击回发?



对此的任何建议都将不胜感激。

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.

推荐答案

后退按钮处理 [ ^ ]



请检查这个,如果,这个是你想要的。

谢谢

:)
Back button Handling[^]

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


这篇关于单击后退按钮时触发postbak的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆