I have an ASP.NET page containing two divs. Both have search fields and a search button contained in each of them. When I first come to this page, Div A has the class "SearchDiv" and Div B has the class "SearchDivDisabled". These classes change their appearance, so the user knows what type of search is currently enabled.
When you click on Div B, JavaScript changes its class to "SearchDiv" and changes Div A to "SearchDivDisabled". It all works like a charm. The problem is when the user goes to Div B, clicks the Div B search button (which obviously redirects to the results page), and then uses the back button. When they return to the search page, Div A turns on again and Div B is turned off, although the last time I used Div B. In the search button event handler, I set the Divs class attribute before redirecting, hoping that this would refresh the pages on the server, so when the user will return, their last allowed Div will still be included (regardless of which one was turned on when the page was first visited).
I believe this is due to ViewState, but I'm not sure why the class attribute is not saved, so when the user returns to the page, it is restored. Is there something I'm missing here, or some simple way to guarantee the behavior I want? Thanks!
Edit: Here is the button event handler code:
protected void RedirectToResults(int searchEnum, string resultPage) { ShowContainer(searchEnum); Response.Redirect(resultPage + this.webParams.getAllVariablesString(null)); } protected void ShowContainer(int searchContainerToShow) { if (searchContainerToShow < 0 || searchContainerToShow > SearchContainers.Count || SearchContainers.Count == 0) return;
RedirectToResults() is called from the actual button event enumerator representing the selected search bar and the URL of the results page. SearchContainers is a dictionary matching an integer with a search div. The important code is the last line, in which I update the selected search container with the "active" search class, and not with the disabled one (which I assign to another div (s))
Additional update: I struggled with this problem for the last couple of days. I could get the following code to work (in page_load):
Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1. Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0. Response.AppendHeader("Expires", "0"); // Proxies.
But this is really not a solution, since everything else that is cached correctly is lost, which leaves me worse than when I started. Everything else, apparently, is preserved well, itβs just a diva class that makes me struggle.
Edit: I just want to update this for anyone who comes across this. Although I believe that there is a solution here to set the page cacheability to force the browser to refuse feedback, I could not get it to work 100% with all browsers (basically Firefox gave me seizures, which is a documented error). I believe the cookie solution will work, but I felt that it could be a little more complicated than necessary, just trying to keep the state of the div pair.
What I ended up was to associate the div class with the state of its correlation switch (there are switches next to the div that allow users a more intuitive way to enable search bars). I noticed that these switches retained the correct controlled value when the back button was used, so I could guarantee that they would indicate that the div was turned on correctly. Therefore, in JavaScript onload, I check which switch is enabled, and then change the classes of the search div accordingly. This is a pretty big hack, but it works 100% in all browsers and takes only about 10 lines of JavaScript.