简体   繁体   English

按“后退浏览器”按钮,失去上一页状态

[英]Press the Back Browser button loosing the previous page state

I am creating a .net application where on the main page I am creating some text boxes dynamically. 我正在创建一个.net应用程序,该应用程序在主页上动态创建了一些文本框。

<div>
Enter how much pets you have : <input type="text" id="amount" />
<input type="button" value="Submit" id="btn" />
 <script type="text/javascript">
 $(document).ready(function()
{
   $("#btn").click(function()
   {
      var i = 0;
      for(i = 0; i <= $("#amount").val(); i++)
      {
         $("#Names").append("<p>Name " + i.toString() + ":<input id='name" + i.toString() + " type='text' class='nameInput'/>");
      }
   });

   $("#SubmitNames").click(function()
   {
      $.getJSON("names.aspx",$(".nameInput"),function(data)
      {
          if(data.Status == "Success")
          {
              window.location.href = "step2.aspx";
          }
      });
   });
});
</script>
</div>

When the user is clicking the submit names it is redirected to step2.aspx 当用户单击提交名称时,它将重定向到step2.aspx。

Till here everything is working fine! 直到这里一切都正常!

The problem starts when the user will go back ("clicks the back browser button") the whole previous status is lost. 当用户返回(“单击后退浏览器按钮”)整个先前状态丢失时,问题就开始了。

The user must enter all again all names. 用户必须再次输入所有名称。

I think I am missing something. 我想我缺少了一些东西。 I need to do the data transmittion via AJAX. 我需要通过AJAX进行数据传输。

Can you help me? 你能帮助我吗? What do you suggest? 你有什么建议?

I am using .NET c# VS2008 我正在使用.NET C#VS2008

Thanks 谢谢

I don't think this is going to work. 我认为这不会起作用。 When you click the back button, it reloads the page from cache. 当您单击后退按钮时,它将从缓存中重新加载页面。 That cache does not include changes you have made via javascript. 该缓存不包括您通过javascript进行的更改。 So it's reloading the base page, then re-executing the javascript, creating a brand new set of empty controls. 因此,它会重新加载基础页面,然后重新执行javascript,创建一组全新的空控件。

Your best is to save the values submitted to a hidden field (probably using json) then have a "go back" button on your next form, then reloading your controls state if the hidden field contains data. 最好的方法是保存提交到隐藏字段的值(可能使用json),然后在下一个表单上使用“返回”按钮,然后如果隐藏字段包含数据,则重新加载控件状态。

EDIT: 编辑:

Since you're not submitting anything, perhaps you can modify your "names.aspx" to return the names entered if you do not pass any data. 由于您没有提交任何内容,因此如果您不传递任何数据,也许可以修改“ names.aspx”以返回输入的名称。 Then in your first step you can do an ajax call to retrieve any names that might be set, doing nothing if no names are returned. 然后,在第一步中,您可以执行ajax调用来检索可能设置的任何名称,如果不返回任何名称,则不执行任何操作。

I should note, however, that using GET in this manner is highly unusual, and according to the HTTP specification get should not change the state of an object (which you seem to be doing). 但是,我应该指出,以这种方式使用GET是非常不寻常的,根据HTTP规范,get不应更改对象的状态(您似乎正在这样做)。

This is because you add a input field by ajax.. The browser dont know what you post For example when you POST and you get to the next page and go back you will get all given information in the fields before you post it.. 这是因为您通过ajax添加了输入字段。浏览器不知道您要发布的内容,例如,当您进行POST并转到下一页并返回时,您将在发布之前获得这些字段中的所有给定信息。

I think even when you cache it, it doenst work.. because for the browser the amount of input field doesnt exists.. 我认为即使您缓存它,也确实可以工作..因为对于浏览器,输入字段的数量不存在。

So what you can try is is instead off using $.getJSON try it with $.ajax({ cache: true }) little example here .. 因此,您可以尝试使用$.getJSON代替$.ajax({ cache: true }) 这里的小例子。

if thats not working i would suggest to set a cookie and check it if there is a cookie when loading the page. 如果那不起作用,我建议设置一个cookie并在加载页面时检查它是否存在cookie。

goodluck 祝好运

Did you inspect the HTTP header's expiration/caching settings that the server sends to the client? 您是否检查了服务器发送给客户端的HTTP标头的过期/缓存设置?

This usually has an influence on whether the page is reloaded or being pulled from the cache when user clicks the "Back" button. 当用户单击“上一步”按钮时,这通常会影响页面是重新加载还是从缓存中拉出。

Try Ben Almans BBQ plugin . 尝试Ben Almans BBQ插件 It is very good. 这很棒。 It utilises the hash tag to persist changes into the browser history cache. 它利用哈希标签将更改持久保存到浏览器历史记录缓存中。 UI tabs demo here . UI选项卡演示在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM