I have a simple dropdown menu that reloads the page with relevant data depending on user selection. I noticed that when I try to go back, the dropdown menu's selected item is incorrect.
For example, if user loads the page for the first time and the dropdown menu has 1a selected and then user selects 2b, the page reloads and the dropdown menu now has 2b selected. Similarly, user selects 3c, the page reloads and now 3c is selected.
But when the user clicks the back button, after the page goes back to the previous state, the drop down menu stays at 3c. If the user clicks the back button again, after the page goes back, the dropdown now shows 2b selected (instead of 1a).
Is this a normal behavior or am I missing something?
Found these but had no luck
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="abcd" runat="server" AutoPostBack="true">
<asp:ListItem>1a</asp:ListItem>
<asp:ListItem>2b</asp:ListItem>
<asp:ListItem>3c</asp:ListItem>
<asp:ListItem>4d</asp:ListItem>
<asp:ListItem>5e</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
There is no surprise here at all.
If you land on that page, and hit back, then you see some other previous page.
ONLY WHEN you cause a post back of the current page does the history start!
The first page load not really part of history. You need a post back for this to occur.
A really nice way to see this in action? Remove the auto post-back, and drop a simple button beside the combo box.
So, say this markup:
<asp:DropDownList ID="abcd" runat="server" >
<asp:ListItem>1a</asp:ListItem>
<asp:ListItem>2b</asp:ListItem>
<asp:ListItem>3c</asp:ListItem>
<asp:ListItem>4d</asp:ListItem>
<asp:ListItem>5e</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server"
Text="test button"
style="margin-left:40px"
/>
So, now when the page loads, note how there is no enabled back button (no history).
You see this, and I can make 2 or 10 combo choices, but no update occurs until I hit the button.
So, no back arrow appears, and history ONLY starts when I have a post back. So, if I FIRST hit the button, then the 1st combo box value is entered into history. But, if I don't make a choice (don't change) the combo, then no history starts.
Thus, for each post back, you have a whole new fresh browser page. However, you do NOT have a post back on the FIRST page load. (So, that first page is not (yet) in your history buffer, is it?).
So, the browser ONLY starts history for a given page when a full new post back has occurred.
If no post back occurred, then back would go to some different previous page.
So, on first page load, you don't have any history! There is nothing to go back to! (and that includes the current page with the current combo box selection).
So, you will in effect lose the first page combo setting, UNLESS you post back that page with the first defaulted choice.
Take off the auto post back, and let the page load, then hit the button with the first defaulted combo box. It will NOW be in your history.
It's better to state that first page load has not been entered into your browser history, and has not (yet) experienced a post back. In fact, what really occurred is the view state not been updated.
So, this is like placing a blank sheet of paper on your desk, and then you start drawing on that page - make changes. OK, now after drawing, you post back, and now pretend that you photo copied the first page. Now you can draw some more, and post back again (photo copy the page).
Now, pull back all the sheets back to the first one - it will have your drawing on it -- it will not be blank, and thus the original combo box choice will also not be on that page.
The stack of previous pages ONLY starts when you post back - so you lose the first combo box, since it was never part of history, but only that first blank sheet load. Only WHEN changes are made to the page (with post-backs) does the history of viewable changes start.
I could get into a complex discussion about view state, but suffice to say that the view state of controls only really gets started and updated on first post back of that given page, not on first page load.