Page_Load not being called on re-navigation - asp.net

I am having a strange problem. Here is the scenario
Here are my files:
Project1.aspx
Project2.aspx
They are set up the exact same, including their Page_Load functions:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If (Not Page.IsPostBack) Then
setPrevIndex(-1)
...
End If
End Sub
They are both set up this way.
Here is where I am running into a problem. When I navigate to either of these pages, i need to make sure that prevIndex is set to -1 (via the function).
For Project1.aspx when I navigate
to the page, the Page_Load fires.
For Project1.aspx when I refresh the page, the Page_Load fires.
For Project1.aspx when I press "Go" in my browsers navigation bar, traveling back to the current page, the Page_Load fires.
For Project2.aspx when I navigate
to the page, the Page_Load fires.
For Project2.aspx when I refresh
the page, the Page_Load fires.
For Project2.aspx when I press
"Go" in my browsers navigation bar,
traveling back to the current page,
the Page_Load doesn't fire at all! The function isn't even
called.
Any ideas why??? What would cause this?
Please ask for clarification.
Update:
When I press "Go" in the URL pointing to the same URL, it seems like the masterpage is the only thing that re-loads, but the Load_Page event doesn't even fire...
Any other suggestions?
Thanks,
E

Try disabling output cache and see if the problem still occurs:
<system.web>
<caching>
<outputCache enableOutputCache="false"/>
</caching>
<system.web>

Use LiveHTTPHeaders or Fiddler to make sure the page is actually being requested the same way each time. This may be an issue with caching.

Load up your website locally and go to http://yourwebsite/trace.axd
This shows a server trace for each page, along with the server status. It also shows the complete page lifecycle with timings.
Clear the current trace and then repeat your 3 visits and reloads each to Project1.aspx and Project2.aspx
What does trace.axd show now? You should have 6 entries, each with the status code of 200 and the verb GET.
If you have fewer then your problem is caching of some sort.
If you have 6 then check the details for the last one - what does is show for the page event lifecycle? It will also show the complete WebForm control hierarchy, so if this related to the master page you will be able to tell.

Try Setting the previndex to -1 in Page Init Event. I am not sure why this is happening though.

Various cache-related things can cause the request not to be made, in particular when you merely press the "go" button, so you should check your cache-headers.
If caching is the issue, you can do something like:
//ask browser to revalidate:
context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
//and hint that the page is outdated anyhow...
context.Response.Cache.SetMaxAge(TimeSpan.Zero);
Which should convince a browser to really get a new version every pageview. You could, for instance, set these variables in the Page_Load itself ;-). If you're not using https, then the following are risk-free too:
//prevents plugin based file-open in IE+https, otherwise fine:
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetNoStore();
I'll bet this solves it - and if not, I second Jeremy Steins suggestion that you use fiddler to verify that the request is really being made at all (and since you're a web-dev, get fiddler in any case, it's a handy tool to have available, and works for all browsers!).
Finally - can you tell whether any other code on the page runs when you click go? (i.e. is the entire page not running, or just Page_Load - the latter would suggest an event wire up error, which would be odd considering your load handler does sometimes fire).

It sounds like your page is cached. This would cause the Page_Load not to fire. Check that you have not set that anywhere.

Have you tried publishing the application to a different machine? It could be IIS doing something so try and eliminate that first. Assuming that your code is identical in both na only the page names differ (do a diff on the aspx and .cs files to verify) then move your application to a different server and re-test.
If it is still occurring the it really must be your browser doing something probably with respect to caching.

Try to recreate the scenario with stripped down functions on your server. If the problem persists, try to use some cache-countering methods. If not, that means it has to be your code.

If you have this (or similar) at the top of your page (or master page) this will cause it:
<%# OutputCache Duration="3600" VaryByParam="none" %>

Related

IsPostback=false even though Request.HttpMethod is "POST"?

I've recently been hacking with webforms seeing if it's possible to use one of my (routing) projects with it. So far, it's been nothing but trouble, but I'm almost to the point that it "works"
I made a page "Test.aspx". At Global.asax, I made it so that it's served at /test instead of /Test.aspx. This works completely. It descends from a custom page class of mine. The custom class finds HtmlForms in the page and rewrites their Action attribute with the proper value: /test.
Now I hit the great brick wall titled Viewstate and ASP.Net events. I added a button to Test.aspx with an OnClick handler. I can click the button, and the page will postback and such, but the OnClick event will not occur. I'm not understanding how a simple URL change can break viewstate like this, as I was not under the impression that Viewstate would track such a thing. Also, IsPostback will be false, even though HttpMethod==true. This is not making any sense to me.
Also, I've disabled EventValidation because I figured that'd be trouble, but this problem persists.
How can I make viewstate and postbacks work as usual when rewriting URLs?
(Note, my form of URL rewriting does all rewriting internally, there is never a HTTP redirect sent to the user)
You might need to tell the HttpContext that the URL is being rewritten as well.
Try doing something like this:
HttpContext.Current.RewritePath("/test");
The IsPostBack is used for check whether the request is from the control of the page itself.
And the HttpMethod is used for check the request type.

Page_PreRender fires twice on first load in session

When I run the web application, I notice that Page_PreRender is fired twice. This only happens the first time in a new session. It does not happen if I refresh the page, or on postbacks.
I use .NET framework 3.5 and the built in ajax functionality.
The problem is not
related to img tag with empty src
attribute (which I have seen other posts with similar problem
has mentioned). I know this because I see this in
both FireFox and IE. The posts I saw
about this stated that this was not a
problem in IE. I have also searched
and found no img tags with empty src
in the generated page source, so it
should not be this.
I have also made a simple test
page where I have included some of
the functionality, and this does not
happen.
Have anyone any suggestions on what happens?
Note:
It is the entire page cycle that is firing twice, not just render.
I've experienced it; it's probably not what you are experiencing, but I'll enter it here anyway; I've noticed it when the application does a Response.Redirect at the PreRender level, which a redirect does not stop current execution, but makes it appear the event happens twice...
Again, probably not related, but including it just in case.
I copied some code that states:
Page.LoadComplete += new EventHandler(Page_PreRender)
I did not realize that this code fire for second time my Page_PreRender event.
When I comment it, never fire twice.

so is it a postback or not

I'm causing a postback by clicking an asp:button. I end up in a breakpoint on Page_Load, where IsPostBack == true. Then, one breakpoint later, I end up in Page_Load where IsPostBack == false.
What the heck is going on? I'm trying to understand someone else's code.
The first link click is a PostBack from the server and the correct behavior. It sounds like the second PostBack is triggered because of a Page.Redirect() or a Server.Transfer() back to the same page.
This is a best guess until you provide additional details.
Here is a blog post that may be helpful.
http://www.craigwardman.com/blog/2009/01/aspnet-multiple-page-load-problem.html
I was having the same issue because the src of one of my images was "" and that caused the page load to fire 2 times.
This sounds like 2 it could be 2 separate requests to the page - Check the IIS logs.
If not then its possibly a Server.Transfer(), or someone might be instantiating and calling the Page_Load method themselves for some reason (extremely unlikely, but easy to check by looking at the callstack)

Visual Studio Debugger. ASP.NET ImageButton Click events not firing

When clicking button in debug mode, the page reloads but the break points inside of the click event are never reached. However, breakpoints in the page_load work just fine.
Basically, it's as if code inside of the button's click event is not being executed.
I checked the site into source control and another developer tried it on his computer. The click events worked just fine there, catching the break points inside.
I tried another site on my computer and it has the same issue. So it's something specific to my computer, and not specific to any site.
Is there some setting I may have mistakenly changed that could cause click events to stop working while debugging?
Any help would be appreciated.
Edited:
This issue is happening on all sites I run in my debugger, and the buttons are not created dynamically.
Edited:
There are no indication of problems when adding the break points. I don't think it's a breakpoint issue, I believe it's an issue with click events not firing. I put identical code on another developers machine and the click event's worked fine while debugging on that machine.
If its something specific to your machine, and after all things fail, I would do a repair on Visual Studio. I've had to do that a couple times when things get weird and it usually works. But remember to exhaust all other avenues first.
How is the button created? If it's added to the page dynamically, remember that this must be done before the load event, or the events won't be wired up. That is the most common reason I've heard for this kind of thing.
Does it say that "symbols will not be loaded" OR "source file is different" when you put the breakpoint inside the button_click event?
If so, you have modified the code files & built the binary (which is different from what site is using).
In my Page_Load event I would check the IsPostBack property to make sure that it is actually a post and not get. I would verify that the Controls collection is populated and all control IDs match those in the Request.Forms.Keys.
The reason for this suggestion is that if you have any add-on in the browser or fancy proxy that messes up with submitted forms and input names are modified, events won't fire for those controls.
I'll take a stab as I remember something similar:
I had upgraded an app from vs2005 to vs2008 and my button code click worked in 2005, but in 2008 I needed to add a "Handles" qualifier at the end of the declaration like this:
Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
for whatever reason the 2005 code didn't need it, but the 2008 required it, and I vaguely remember having the very same symptoms you are describing.
So it seems all the click events I was testing were image buttons, and I didn't have the images on my local machine. For some reason, when the image was missing, the click event wouldn't fire.

Random Page_Load calls on back button in ASP.NET

I'm hoping someone has seen this before because I can't for the life of me find the problem.
I'm trying to do the old "fix the back button" thing in an application and I think i have a pretty decent approach, the problem is that it relies on the application not calling page_load when you hit back and instead loading the cached version of the page.
On about 60% of my pages that's exactly what happens. It loads the cached version and all is well. On the other 40% when i hit the back button page_load calls, forcing a refresh. For reference the call to page_load is NOT in a postback.
Even stranger is that this only occurs in IE (6 & 7). In firefox page_load never gets called.
I am using ASP.NET Ajax framework on both types of pages. Anyone seen anything like this before?
--Update--
After investigating a bit more I'm finding out that when i use the search to navigate from one page to another the application behaves differently for different pages. On the broken pages the page_load gets called twice, the search gets called twice and in fiddler that turns into 2 different redirect postbacks the second of which has no-cache set.
On the working page page_load and search only happen once and it immediately redirects.
That second Response.Redirect is causing the issue. Still not sure why that's happening though.
Check what the server is returning for the cache-control http header, then try setting Response.Cache.SetCacheability()/ use the output cache page directive on the pages and see if the server is saying that the pages should be cached.
if you are using ASP.NET AJAX why not using the History server control object?
replacing History, the back button will go to the link you want.
try this

Resources