5

I have this DropDownList inside a DataList.

<asp:DropDownList runat="server" ID="DDL_ProdCat" OnSelectedIndexChanged="DDL_ProdCat_SelectedIndexChanged" 
                  Autopostback="true" DataTextField="Name" DataValueField="ID" />

When the user makes a selection in this DropDownList, for some selections, they are redirected to a separate page.

After being redirected, the user hits their browser-back button, they are returned to this page with the DropDownList.

Unfortunately, the selection which redirected them to the new page is still selected.

Example

  • DDL contains A,B -- Initial Selected Value: A
  • User selects B -- Postback redirects them to another page
  • User hits "back" on browser
  • The page now shows "B" as being selected while the page-state suggests that "A" should still be selected. The page can never be in the "B" state, because "B" is marked to redirect users to that other page.

Is there a way to reset the DropDownList selection to a particular value when the user revisits the page via the browser-back button?

Note

  • I am forced to use a DDL here, because the common-case is that a redirect does not occur. I understand it is generally not the best option for linking users to other pages.
  • Unfortunately, I am unable to turn off browser-caching for the entire page for performance reasons
Brian Webster
  • 30,033
  • 48
  • 152
  • 225

3 Answers3

4

If it is OK to remove page-level browser-caching, you can try to remove the cache so that it reloads the page when the user goes back. Add this to page load:

Response.Expires = 0
Response.Cache.SetNoStore()
Response.AppendHeader("Pragma", "no-cache")
Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Paul Graffam
  • 2,139
  • 2
  • 18
  • 20
  • Oh, didn't notice that you couldn't turn off caching. Disregard then. – Paul Graffam Aug 11 '11 at 14:15
  • What's the difference between this and Response.Cache.SetCacheability(HttpCacheability.NoCache)? – James Johnson Aug 11 '11 at 14:17
  • Thanks for the advice Paul, I was not clear in my initial question regarding page-level caching – Brian Webster Aug 11 '11 at 14:23
  • 2
    @James, Response.Cache.SetCacheability(HttpCacheability.NoCache) only works for IE I believe. [Link](http://stackoverflow.com/questions/914027/disabling-browser-caching-for-all-browsers-from-asp-net) – Paul Graffam Aug 11 '11 at 14:25
  • @hamlin11 you could have a session boolean in your 2nd page that's set to true and if user goes back to 1st page have that boolean used in if statement to deselect ddl. – Paul Graffam Aug 11 '11 at 14:31
  • Paul, unfortunately the page-init and page-load functions are not hit upon browser-back button on the original page. Now, if I could cause a postback to occur, then I'm in my comfort zone. However, I just checked w/ breakpoints -- page load & page init not hit upon postback. I like the idea, but it might need to be accomplished upon browser-back w/ jquery – Brian Webster Aug 11 '11 at 14:34
0

Here's the code that worked for me:

<script type="text/javascript">
  window.onload = function () {
    var ControlValue = document.getElementById("<%= DropDownList.ClientID %>");
    if (ControlValue.value != origControlValue) {
      ControlValue.value = origControlValue;
    }
  }
  var origControlValue = document
    .getElementById("<%= DropDownList.ClientID %>").value;
</script>
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
user955879
  • 33
  • 5
0

As suggested, use JavaScript on the page to re-set the selection in the drop down.

Leon
  • 3,311
  • 23
  • 20