开发者

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 sugge开发者_如何转开发sts 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


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")


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


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜