Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET - DropDownList contains wrong value upon Browser-Back-Button

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
like image 712
Brian Webster Avatar asked Oct 11 '22 07:10

Brian Webster


1 Answers

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")
like image 142
Paul Graffam Avatar answered Oct 12 '22 19:10

Paul Graffam