views:

500

answers:

4

Let's say I have two pages on the same ASP.NET C# WebSite.

  • Page1.aspx does things in the Page_Load event
  • I navigate to Page2.aspx using the menu
  • Page2.aspx does some things then Response.Redirect back to Page1.aspx
  • Page1.aspx cannot do things in Page_Load event this time because it never fires.

I tried to turn off cache declaratively, tried using true for endResponse in my redirect... nothing seems to make a difference.

Never mind everybody! I am a moron! Using Visual Studio Dev Localhost the Redirect was redirecting to the live page! :)

+3  A: 

When you navigate to a page using the Back button, the page is reloaded from memory, and no request is sent to the server.

You can confirm this using Fiddler.

I'm not sure if this is true in all browsers.

SLaks
I am not using back button. :(
G Berdal
+2  A: 

If you are redirecting, it's possible the client is caching the response. In order to get past this you might add an extra query parameter that simply holds the time.

This is usually enough to get past most pages caching mechanisms.

Chris Lively
Some reason this did not work either...
G Berdal
You need to change your browser settings to get rid of caching. In IE you should have it set to Check For Newer versions "Every time I visit the webpage" or at the very least "Automatically". The "Never" settings is (pun intended) Never a good idea.
Chris Lively
I turen off cache in IE, no joy. I'm beginning to think this might not be a caching problem...
G Berdal
+1  A: 

The reason for the page executing doesn't affect the page cycle, the Load event always fires when the page is executed.

So, if the Page_Load doesn't run sometimes, it's because the page is cached and doesn't execute on the server. The page can be cached in the browser, in a router somewhere along the way, or on the server using server side page caching.

If you haven't enabled server side page caching for the page, it's cached in the browser or in the network. You can use cache settings to try to elliminate this:

Response.Cache.SetCacheability(HttpCacheability.NoCache);

This will keep the page from being cached in normal circumstances. (Check also that your browser isn't in offline mode, then it will use anything in the cache regardless of it's cacheability settings.)

Guffa
I used <%@ OutputCache Location="None" VaryByParam="None" %> on the page which I believed to have the same effect. However, it doesn't seem to make a difference.
G Berdal
I think if it wasn't for my screw up, this would have been the closest answer to soloving this problem so I accept this as the solution. However, thanks all of you for trying to help.
G Berdal
@G Berdal: Note that preventing the output cache doesn't prevent the page being cached in the browser or in the network.
Guffa
A: 

Try using Server.Transfer instead of Response.Redirect.

The client will not see the URL change but this may not matter, depending on your requirements

D3no
In this case the Page_Load event fires. However, because I am using UpdatePanels and some reason this ends up in a Sys.WebForms.PageRequestManagerParserErrorException.
G Berdal