I’ve created a simple example to show what I’m having problems with. Place a asp.net checkbox on a page and an asp.net textbox on the same page. In the code behind (using vb.net), put code that displays something in the textbox when the checkbox is checked and displays an empty textbox when the check box is not checked. Simple, right? Now toggle back and forth several times with the checkbox checked/unchecked and the textbox has content/no content. Everything works fine – right?
Now click your back button on your browser a few times. The results being displayed with each back button click is opposite – check box is checked yet nothing in the textbox. Checkbox is unchecked and there is text in the textbox. It doesn’t matter where you put the vb code (page load, checkedchanged) – same results.
I’m thinking that it has something to do with the timimg of the page being saved in cache. Something like the page is being saved in the cache while the checkmark is firing the “click” event yet the code to fill the textbox has not yet been fired. Then again I could be completely off the mark and it is something real simple that I’m doing wrong.
Any ideas/thoughts/help would be greatly appreciated.
Sample Code:
ASPX:
<asp:CheckBox id="ChkBox1" runat="server" AutoPostBack="True" text="Sample" /><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
VB:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If ChkBox1.Checked = True Then
TextBox1.Text = "Checked"
Else
TextBox1.Text = ""
End If
End Sub
This is the way it should be.
Browsers remember every time you post back to the server. Your page is posting back whenever the checkbox state changes.
When the checkbox state is checked, the textbox is indeed empty. This is when the postback occurs. Your server side code sets the text value, and you see it in the browser.
But your back will remember what you sent, that was a checked checkbox and empty text.
The reserve case happens when you uncheck.
If you refresh one of those back pages with a F5, your browser should ask to send back post data. And you will get back to the expected state.
Tell the browser not to cache the content(if you want the browser to forget it):
Not VB code, but I am sure you will find similar functions on VB.net too.