article

Fix for ASP .NET Web Forms acting 'buggy' after user presses 'back' button

Email
Submitted on: 1/1/2015 2:40:00 AM
By: Ian Ippolito (PSC)  
Level: Beginner
User Rating: By 3 Users
Compatibility: VB.NET, ASP.NET
Views: 2124
author picture
 
     If you do ASP.NET web forms, have you noticed that they seem to act buggy after the user hits the back button? For example, do events start firing off that shouldn't be, or does the state on the form get reset to what the user typed in 2 times ago, or worse? It's not a bug...and here's how to fix it.

 
				I ran into this problem 
while creating a website that had a _change event coded.  It worked fine 
the first time through, but whenever the user hit the back button, the _change 
event would fire even though the user hadn't changed anything.

I discovered the problem was NOT a bug. Instead it is caused by a peculiar interaction between the ASP.NET view state mechanism (which allows pages to 'remember' your control states) and the browser's cache, which in my case was IE 6.0.

Basically, the browser overrode the HTML rendered by ASP.NET's view state mechanism with its cached contents. This might be fine on old style ASP pages, but with ASP.NET web forms, the fact that the cache 'remembered' the new setting of my control, forced the _change event to fire, even though the user hadn't selected it after hitting back.

Once you understand the problem, the fix is simple, just disable the browser's caching as the first line in your Page Load. You should do this for all ASP.NET form pages that have event handling. Turning off caching won't affect performance, because these type of pages load dynamically every time, anyway.

Hope this saves you some headaches!

Response.Cache.SetExpires(Now())
Response.Cache.SetCacheability(HttpCacheability.NoCache)


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.