Why FireFox cannot keep my state but IE can?

Last post 07-05-2007 4:51 AM by stmarti. 11 replies.

Sort Posts:

  • Why FireFox cannot keep my state but IE can?

    06-28-2007, 9:11 AM
    • Loading...
    • JohnnyCui
    • Joined on 06-28-2007, 1:01 PM
    • Posts 3

    Hi,

    My problem happens when user leaves my page and then he/she want to go back using the “Back” button in browser. Since I want to save some data in page’s hiddenfield during the async postback so that I can dynamically reconstruct the DOM when user come back. The problem is FireFox can not keep the hiddenfield’s value if user modified the DOM before he/she leave the page(of course, IE can).

    You can run the following code in a single page WebForm with Microsoft AJAX enabled.

    Can you help me to work around on FireFox?

    Thanks,

     

    1    <%@ Page Language="C#" %>
    2
    3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4
    5 <script runat="server">
    6 protected void Button1_Click(object sender, EventArgs e)
    7 {
    8 if (ScriptManager1.IsInAsyncPostBack)
    9 {
    10 ScriptManager1.RegisterDataItem(HiddenField1, "HereIAm");
    11 }
    12 }
    13 </script>
    14
    15 <html xmlns="http://www.w3.org/1999/xhtml" >
    16 <head runat="server">
    17 <title>Can not keep state in FireFox after navigate back in browser.</title>
    18 </head>
    19 <body onload="pageLoader()">
    20 <form id="form1" runat="server">
    21 <div>
    22 <asp:ScriptManager ID="ScriptManager1" runat="server">
    23 </asp:ScriptManager>
    24
    25 <script type="text/javascript">
    26
    27 // add handler for pageloading event for async call
    28 Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(PageLoadingHandler);
    29
    30 // pageloading event handler
    31 function PageLoadingHandler(sender, args)
    32 {
    33 var dataItems = args.get_dataItems();
    34 if ($get('HiddenField1') !== null)
    35 {
    36 $get('HiddenField1').value = dataItems['HiddenField1'];
    37 }
    38 alert('Async get HiddenField1.Value= ' + $get('HiddenField1').value);
    39 }
    40
    41 function pageLoader()
    42 {
    43 alert('onLoad: HiddenField1.Value = ' + $get('HiddenField1').value);
    44 }
    45
    46 // When this event fire, we add some new element into the DOM.
    47 // After I get the hiddenfield's state, I leave current page to somewhere
    48 // and then come back using 'Back' button of browser.
    49 // FireFox will not keep the hiddenfield's value but IE can. Why???
    50 function inputClick()
    51 {
    52 var divDynamic = $get('dynamicArea');
    53 var table = document.createElement("table");
    54 var row = document.createElement("tr");
    55 var col = document.createElement("td");
    56
    57 var inputSL = document.createElement("input");
    58 inputSL.type = "text";
    59 inputSL.id = 'dyn1';
    60 inputSL.value = 'A';
    61
    62 col.appendChild(inputSL);
    63 row.appendChild(col);
    64 table.appendChild(row);
    65
    66 if(document.all)
    67 {
    68 divDynamic.innerHTML = table.outerHTML;
    69 }
    70 else
    71 {
    72 divDynamic.appendChild(table);
    73 }
    74 }
    75 </script>
    76
    77 <input id="input1" type="button" value="Add a textbox." onclick="inputClick()" />
    78 <div id='dynamicArea'></div>
    79 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    80 <ContentTemplate>
    81 <asp:Button ID="Button1" Text="Get HiddenField state." runat="server" OnClick="Button1_Click" /> <br />
    82 </ContentTemplate>
    83 </asp:UpdatePanel>
    84 <asp:HiddenField ID="HiddenField1" runat="server" />
    85 <a href="http://ajax.asp.net">Leave this page. Then go back using browser's BACK button.</a>
    86 </div>
    87 </form>
    88 </body>
    89 </html>
    90

    Filed under: , ,
  • Re: Why FireFox cannot keep my state but IE can?

    06-28-2007, 9:33 AM
    • Loading...
    • JoeStagner
    • Joined on 11-20-2002, 10:14 PM
    • USA
    • Posts 38
    • AspNetTeam
      Moderator

    Hi Johnny,

    Whay not store the data in a cookie with Javascript ?

     Joe

    [ I am NOT able to respond to PMs - please contact me via www.MisfitGeek.com ]
  • Re: Why FireFox cannot keep my state but IE can?

    06-28-2007, 9:46 AM
    • Loading...
    • JohnnyCui
    • Joined on 06-28-2007, 1:01 PM
    • Posts 3

    To Joe: Yep, you're right.

    But it is still possible that user disable the cookie in browser.

    Do you know what's happened in my problems?

  • Re: Why FireFox cannot keep my state but IE can?

    07-02-2007, 8:23 PM
    • Loading...
    • Richard Line
    • Joined on 07-02-2007, 9:33 PM
    • Staffordshire, UK
    • Posts 33

    Firefox is reloading the page from the server and IE is reloading from the cache.

    You could try and store the value in a session object and do something like <META Http-Equiv="Expires" Content="0"> so the page reloads from the server and check the session object

    Richard Line

    Please click "Mark as Answer" on the posts that help you.
  • Re: Why FireFox cannot keep my state but IE can?

    07-02-2007, 11:24 PM
    • Loading...
    • eriawan
    • Joined on 01-25-2007, 3:46 AM
    • Posts 30

    JohnnyCui: If you want to use cookieless session, you can use the <sessionstate> settings with added attribute of cookieless and UseUri value in your web application web.config file.

    for example from MSDN Library:

    <sessionState 
       cookieless="UseUri" 
       useHostingIdentity="true">
    </sessionState>

    The disadvantage is, user can see the session URI in the location bar of your browser. The best choice is using the AutoDetect value for cookieless attribute. This works forany kinds of cookies supported or unsupported browsers. CMIIW.

     

  • Re: Why FireFox cannot keep my state but IE can?

    07-03-2007, 6:07 AM
    • Loading...
    • JohnnyCui
    • Joined on 06-28-2007, 1:01 PM
    • Posts 3

    Richard Line:

    Firefox is reloading the page from the server and IE is reloading from the cache.

    You could try and store the value in a session object and do something like <META Http-Equiv="Expires" Content="0"> so the page reloads from the server and check the session object

     To Richard: but I still confused why the firefox behave differently on the state. Does it because I change the DOM when invoke the async post back?

  • Re: Why FireFox cannot keep my state but IE can?

    07-03-2007, 6:18 AM
    • Loading...
    • Richard Line
    • Joined on 07-02-2007, 9:33 PM
    • Staffordshire, UK
    • Posts 33

    With out testing it myself (at work) my guess would be that by going back in firefox it is requesting the page afresh from the server, so no saved hidden value. But in IE it is loading it from its cache which is holding the state of the page at the point that you navigated away from it.

    You may be able to get IE to behave the same by changing this setting: Goto (in IE)
    Tools
    Internet Options
    Browsing History --> Settings
    and select 'Every time I visit the page'

    Hope that helps

    Richard Line

    Please click "Mark as Answer" on the posts that help you.
  • Re: Why FireFox cannot keep my state but IE can?

    07-04-2007, 4:54 AM
    • Loading...
    • stmarti
    • Joined on 06-06-2006, 8:20 AM
    • Posts 754

    Richard Line:

    With out testing it myself (at work) my guess would be that by going back in firefox it is requesting the page afresh from the server, so no saved hidden value. But in IE it is loading it from its cache which is holding the state of the page at the point that you navigated away from it.

     

    I think this is not true. I've tested the original sample, and firefox works fine until the dom is not altered (even the ajax callback doesn't matter when the dom not modified). So the hidden value also saved in firefox!

    The problem is altering the dom. This is strange, maybe this is a firefox bug or this behaviour is recommended by standards? I don't know. 

  • Re: Why FireFox cannot keep my state but IE can?

    07-04-2007, 5:04 AM
    • Loading...
    • Richard Line
    • Joined on 07-02-2007, 9:33 PM
    • Staffordshire, UK
    • Posts 33

    Hi stmarti

    My comments were about the behaviour when using the back button, not general page use, as using the back button is not a normal navigation but a request to the browser to move back 1 step in the history of the browser session.

    Richard Line

    Please click "Mark as Answer" on the posts that help you.
  • Re: Why FireFox cannot keep my state but IE can?

    07-04-2007, 5:22 AM
    Answer
    • Loading...
    • stmarti
    • Joined on 06-06-2006, 8:20 AM
    • Posts 754

    Hi Richard, 

    I just try to say that the back button behavior is the same for firefox and ie until the dom is not modified in firefox. This is an interesting problem. So "going back in firefox it is requesting the page afresh from the server" is not true with default firefox settings. I'm not sure maybe I'm wrong about this.

    The cache settings can be set similar to ie (which you already described) by using about:config for address and Browser.cache.check doc frequency - MozillaZine Knowledge Base

    The best solution for the original problem is implement some server side back button mechanism which support ajax/async postbacks. There are tons of articles about this.

  • Re: Why FireFox cannot keep my state but IE can?

    07-04-2007, 4:28 PM
    • Loading...
    • Richard Line
    • Joined on 07-02-2007, 9:33 PM
    • Staffordshire, UK
    • Posts 33

    My mistake, I have messed with the setting in both browsers that much forgot which setting where default for firefox.

    If you implement a back button on the page you still get the potential of someone clicking the browsers back button and client side settings being lost. So designing for that is surely more robust?

    Richard Line

    Please click "Mark as Answer" on the posts that help you.
  • Re: Why FireFox cannot keep my state but IE can?

    07-05-2007, 4:51 AM
    • Loading...
    • stmarti
    • Joined on 06-06-2006, 8:20 AM
    • Posts 754

    Richard Line:
    If you implement a back button on the page you still get the potential of someone clicking the browsers back button and client side settings being lost. So designing for that is surely more robust?
     

    Richard, you misunderstand what I tried to say, but that is my fault (my poor English) Smile

    So it's possible to support the borwser's back button even when we use ajax/partial postbacks. Of course that needs additional programming. There are lots of article about this, also worth to search on these forums.

    Anyway I've never implemented such thing (there is no real benefit using back button in my apps), so I have no experience how robust is this.

Page 1 of 1 (12 items)
Microsoft Communities