I have an ASP.NET 4.0 application that uses CrossPagePostback. I just noticed that my originating page is being loaded twice and I can’t find the reason why. Here is the general logic:
BasePage.vb:
Public Class BasePage
Inherits System.Web.UI.Page
Protected Overrides Sub OnPreInit(e As System.EventArgs)
If IsCrossPagePostBack Then Exit Sub
...
Common code
...
MyBase.OnPreInit(e)
End Sub
Protected Overrides Sub OnInit(e As System.EventArgs)
If IsCrossPagePostBack Then Exit Sub
...
Common code
...
MyBase.OnInit(e)
End Sub
Protected Overrides Sub OnLoad(e As System.EventArgs)
If IsCrossPagePostBack Then Exit Sub
...
Common code
...
MyBase.OnLoad(e)
End Sub
End Class
Page1.vb.aspx:
Public Class _Page1
Inherits BasePage
Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
...
Page1 specific code
...
End Sub
End Class
Page2.vb.aspx:
Public Class _Page2
Inherits BasePage
Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
...
Page2 specific code
...
End Sub
End Class
In Page1.aspx I have an ASP LinkButton with PostBackUrl set to Page2.aspx. I set breakpoints in OnPreInit, OnInit and OnLoad in BasePage.vb. When the LinkButton is clicked in Page1.aspx, all 3 page events fire for Page1.aspx – IsCrossPagePostBack=True, which is what I expect for the originating page. Then all 3 page events fire for Page2.aspx and IsCrossPagePostback=False which is again expected.
I would expect both pages to be done firing these page events at this point. I can even see that Page2.aspx has rendered and is visible in my browser, but for some reason Page1.aspx page events are once again fired and this time IsCrossPagePostback=False which allows undesired code to run for the originating page.
If you weren’t setting breakpoints and debugging this code you’d never know it was happening. There aren’t any noticable adverse effects from this happening, except that it’s costing processing time and I KNOW it’s wrong. I just can’t determine what’s causing Page1.aspx to be loaded again after Page2.aspx is loaded.
In my Call Stack window there is no caller, only a single line containing the current Sub (OnPreInit, OnInit or OnLoad).
I’ve traced through the code step by step. I get to the point that Page2.aspx is fully loaded and then end up in the page event for Page1.aspx again.
I also have a LinkButton control in Page2.aspx that has its PostBackUrl set for Page1.aspx – yet don’t have this problem.
Does anyone know what might cause this or how to debug it? I’ve seen a few posts that say empty img tags will cause it, but that’s not the problem here.
EDIT:
After an all-nighter of debugging and research, I’ve determined that my originating CrossPagePostback problem is in fact being caused by an IMG tag with a missing image. This is a big problem for me as the IMG SRC= URL is coming from a data received in a REST response I have no control over.
The REST response contains lots of data, some of which are URLs to images relating to a product. The images are essential to the page information. There are millions of products and it just happens that I noticed this problem on a product that has missing images. 99.99% of all the products have valid URLs for the images, so this is not going to be a common problem.
Yet, I really don’t want my originating page to be loading twice and I’d like to stop it from happening. Is this a bug that an originating page in a CrossPagePostback scenario will load twice if an IMG SRC= is broken in the target page?? Is there a fix??
Am going to do some more research, but figured I’d post a followup to my question right away. Anyone know of a solution/fix? I’ve read through similar posts here on SO that a missing image src will cause an originating CrossPagePostback page to load twice. The answer always seems to be either correct the src URL or make the image available so this don’t happen. Unfortunately, I don’t have either of those options – the img SRC is absolutely correct as is provided in the REST response received and the image is not mine, it’s supposed to be on a remote server as specified in the REST response.
Aside from checking the existence of each individual image before rendoring my page, I don’t know of a workaround. Checking for existance just isn’t a good option.
After an all-nighter of debugging and research, I’ve determined that my originating CrossPagePostback problem is in fact being caused by an IMG tag with a missing image. This is a big problem for me as the IMG SRC= URL is coming from a data received in a REST response I have no control over.
The REST response contains lots of data, some of which are URLs to images relating to a product. The images are essential to the page information. There are millions of products and it just happens that I noticed this problem on a product that has missing images. 99.99% of all the products have valid URLs for the images, so this is not going to be a common problem.
Yet, I really don’t want my originating page to be loading twice and I’d like to stop it from happening. Is this a bug that an originating page in a CrossPagePostback scenario will load twice if an IMG SRC= is broken in the target page?? Is there a fix??
Am going to do some more research, but figured I’d post a followup to my question right away. Anyone know of a solution/fix?
Thanks.