ASP.NET after returning from the wrong list down after using the browser Back button

I have an ASP.NET 2.0 web application that is partially navigated using controls DropDownList. When one of them is changed, it causes a postback, which then redirects to another URL, depending on what was selected from the drop-down list.

I noticed strange behavior when using the "Back Browser" button after using the dropdown menus. The procedure is as follows:

  • Make a selection in one of the drop-down menus, causing postback and redirection. Still.
  • Click the back button of the browser.
  • In the other drop-down menu below the one used before (they are all in one div), make a choice. The page is redirected to the same URL as the first time, and not to what it should redirect to based on this other drop-down list.

I tried this in both Firefox 10 and IE9 and saw the same thing. I looked at the Net tab in Firebug and saw that the correct control was specified in POST for step 3. But when I debug it, the event handler for the wrong drop-down list (the one used in step 1) is triggered.

The code is pretty simple and straightforward. Layout Example:

<asp:DropDownList runat="server" ID="ddlTest" AutoPostBack="true" />

Drop-down lists are not really simple elements <asp:DropDownList ... />; I am inserting elements optgroupwith an approach like this .

C # example:

ddlTest.Click += new EventHandler(ddlTest_SelectedIndexChanged);

ddlTest_SelectedIndexChanged:

if (ddlTest.SelectedValue != "")
{
   Response.Redirect(MyUtilClass.GetUrl(ddlTest.SelectedValue));
}

?

2/6/2012. , Request["__EVENTTARGET"] SelectedIndexChanged. , . ? , postback ?

+5
4

, "" , , , / / , , DropDownlist "" .

onload IE ( .: http://dean.edwards.name/weblog/2005/09/busted/) , "" , , , JS, , , onload , , , , , :

page1.aspx

 <script type="text/javascript">
        function redir(){    
            var h=document.getElementById('hdR');
            if (h.value=='1'){
                h.value='0';
                document.location.href='page1.aspx';
            }         
        }

        function changeHids(){
            var h=document.getElementById('hdR');
            h.value='1';
        }
        </script>
    </head>
    <body onload="redir();">
        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" onchange="changeHids();">
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
                <asp:ListItem>3</asp:ListItem>
                <asp:ListItem>4</asp:ListItem>
            </asp:DropDownList>
            <br />
            <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" onchange="changeHids();">
                <asp:ListItem>5</asp:ListItem>
                <asp:ListItem>6</asp:ListItem>
                <asp:ListItem>7</asp:ListItem>
                <asp:ListItem>8</asp:ListItem>
            </asp:DropDownList>
            <asp:HiddenField ID="hdR" runat="server"/>
        </div>
        </form>
    </body>

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

    Response.Redirect("page2.aspx?ddl=1&val=" & Me.DropDownList1.SelectedValue, True)

End Sub

Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged

    Response.Redirect("page2.aspx?ddl=2&val=" & Me.DropDownList2.SelectedValue, True)

End Sub

page2.aspx

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Response.Write("ddl:" & Request.QueryString("ddl") & " " & "value:" & Request.QueryString("val"))

    End Sub
+3

Firefox - :

protected void Page_Load(object sender, EventArgs e)
{ 
    Response.AppendHeader("Cache-Control", "no-store");
}

Chrome - javascript pageload:

var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if (is_chrome) {
    document.getElementById("form1").reset();
}
+3

Response.Cache , . MSDN SetAllowResponseInBrowserHistory

HttpCacheability NoCache ServerAndNoCache, HTTP- -1; "", / . , SetAllowResponseInBrowserHistory allow true.

, .

 protected void Page_Load(object sender, EventArgs e)
  {
       Response.Cache.SetCacheability(HttpCacheability.NoCache); 
       Response.Cache.SetAllowResponseInBrowserHistory(false);

        // or else you can do like this 

       Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
        Response.Expires = -1;
        Response.CacheControl = "No-cache";
  }

, , t .

+2

A slight variation of the answer from @Ravi worked for me ...

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    Context.Response.AddHeader("Cache-Control", "max-age=0,no-cache,no-store");
    Context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Context.Response.Cache.SetMaxAge(TimeSpan.Zero);

    ....
}
0
source

All Articles