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.
Related
I have a simple timer method in vb, that currently does nothing, it just ticks. Once it does however, all my other code on the page stops working.
as an example, I have image buttons on my page that add controls to a static place holder.
btnCreate.Text = "Create"
btnCreate.ID = "btnCreateSpecialNotes"
AddHandler btnCreate.Click, AddressOf btnCreateSpecialNotes_Click
plhCreateSpecialNotes.Controls.Add(btnCreate)
so without the timer.tick method, that (along with other code not included) would fire off as expected and do what I want, but when the timer.tick happens, everything sort of freezes and nothing works.
My timer is set up as follows
<asp:Timer ID="specialNotesTimer" runat="server" Interval="2000" ontick="specialNotesTimer_Tick"></asp: Timer>
and in the code behind...
Protected Sub specialNotesTimer_Tick(Byval sender as object, Byval e as eventArgs) Handles specialNotesTimer.Tick
'Do things to the page
End Sub
DISCLAIMER: I have never used the System.Web.UI.Timer class.
I think there might be some confusion between client side javascript code and server side C# code.
After reading MSDN, it seems that the Timer control would initiate a full postback every 2000 milliseconds (2 seconds because you say so above). This can only be done in javascript and there needs to be a server side event handler that will perform some task on the server (you call it specialNotesTimer_Tick).
Now, if this task takes longer than 2 seconds to execute, I would assume that you will never see any information on the web page because it would constantly be postbacking (posting back?) and refreshing the screen.
Suggestions:
Reconsider your approach for using a timer
Increase the timer interval
Add an UpdatePanel so the processing happens asynchronously, thus avoiding the screen refresh
Hope this helps.
I think the main problem I had here was the flow of my html wasn't ending. I restarted my page from scratch slowly, and though I used virtually the identical code, I just triple checked all my close tags and what not in html, and that seemed to solve the problem itself.
I'm sorry there isn't a more in depth answer, but I still don't understand why my old code here wasn't working, but re-doing my page from square one worked for me.
My module's Page_Load event is firing twice for each "actual" load. On the initial load both loads' Page.IsPostBack property is false.
I've renamed Page_Load to Module_Load to verify the name wasn't an issue. I've made sure the method isn't handling both Me.Load and MyBase.Load, which has been the case in the past.
The only thing I'm doing out of the ordinary is that my module is inheriting from an intermediate base class. Could this be the culprit?
My module:
Namespace Modules.RedactedNamespace
Public Class List
Inherits RedactedModuleBase
Protected Sub Module_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.Page.IsPostBack Then
BindList()
End If
End Sub
End Class
End Namespace
My base:
Namespace Modules.RedactedNamespace
Public MustInherit Class RedactedModuleBase
Inherits DotNetNuke.Entities.Modules.PortalModuleBase
End Class
End Namespace
Edit (This fixed it) - I had an Image without an ImageUrl. Presumably this is set by my CollapsiblePanelExtender but rendered with a blank src.
This can happen if you have an img tag with an empty src attribute.
I know this sounds strange, but I believe it has to do with the web browser trying to figure out how to load the image with a blank SRC.
I don't know the protocols involved, but I'd bet there is some ambiguity regarding how to resolve empty string.
So, in the case of some browsers, it actually fires a web request to the current URL hoping the image comes back.
Sounds like a reasonable assumption, but it just so happens to break many ASP.Net web forms.
Asp.net page_load function is loading twice.. hence it affects my page performance.
Does anyone know the reason it is loading twice.
No, iam not calling the page load function anywhere...
Just ran into this problem, and thought I would post an answer summarizing what I found, plus my actual issue.
1. img tags with src="" or Image tags with ImageUrl=""
2. Using AutoEventWireup="true" and adding a page handler
3. Having manually added the event handler (more common for C# than VB)
4. Handling both MyBase.Load and Me.Load
5. Variation on the missing img src, body { background-image: url(); }
6. Rewrite rule and missing favicon.ico
and finally my issue....
My page inherited from a class that included a Page Load handler, which inherited from a class with a Page Load Handler.
Public Class C1
Inherits System.Web.UI.Page
Protected Overridable Sub PageLoad(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
Public Class C2
Inherits C1
Protected Overrides Sub PageLoad(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.Load
MyBase.PageLoad(sender, e)
End Sub
End Class
Public Class MyPage
Inherits C2
Protected Overrides Sub PageLoad(ByVal sender As Object,
ByVal e As System.EventArgs)
MyBase.PageLoad(sender, e)
End Sub
End Class
I tested this, and if you put a Handles on the method in MyPage, it will get hit 3 times...
It is not you calling the page load function twice, that is the way ASP.NET works. The page posts to itself, thus calling the page_load function, when any Server controls on the page are fired (those which as set to postback).
What you need to do is to put some checks to differentiate between an initial page load and a post back
if(!IsPostBack)
{
//Code when initial loading
}
else
{
// code when post back
}
Please find the solution here........
Check if the Load events have Handlers for Base class and the child class
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load, Me.Load**
If it is just Remove the Me.Load from the event , now check your page. Hope this may be useful and solve your issue.
Once I found the following string in a project:
<link rel="Shortcut Icon" href="#" type="image/x-icon" />
Somebody just did it like he usually does with "a href".
But browser actually tries to get the site icon on each refresh, so it sends a request to the address from href parameter, i.e. to the same page.
So, check this as well.
For me It was a blank image tag.
<img src="#" />
I solved my issue by setting the AutoEventWireUp attribute to FALSE. I got this issue when migrating from .net 1.1 to .net 4.0. Somehow VS2012 reset this attribute to TRUE when I copy the file over from the older version.
Remember to check the IsPostBack value as shown below:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
You can put breakpoints inside this IF block to verify you running Page_Load twice.
If you are seeing Page_Load run twice and each time it is not a postback, then check the OnInit() method for this page.
Verify you are not wiring up the Load handler like below. You will see this code often from code that was migrated from earlier versions of Visual Studio.
this.Load += new System.EventHandler(this.Page_Load);
Remove this if you find it. This assumes that you have the following at the top of the markup for the page.
AutoEventWireup="true"
I had the same problem and Solved.
I Checked my Global.ascx and My rewrite rules.
When the page requested, URL did not have "/" at the end of URL and a redirect happened from "x.com/x" to "x.com/x/" according to my configuration for SEO standards.
So anything works well and your internal links should have "/" at the end of URLs to avoid multiple loads.
The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page.
Use the OnLoad event method to set properties in controls and to establish database connections.
Reffer MSDN: enter link description here
Please try making the changes mentioned in this link.
http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/ccc75925-3460-497c-8471-fcebecd8d061
BTW I googled Page_Load Being called twice
I had the same issue.
It was because of a TreeNode with ImageUrl="".
For me I could get around this calling multiple times by using the PreRender event instead
protected override void OnPreRender(EventArgs e)
This is only called once, even if the onload's and init's are called a million times.
I replaced the Response.Redirect with Server.Transfer because it was suggested to be the new way of doing things. Since then, the pages load twice and the Back-button in Chrome returns to the previous page and immediately back to the current page. I replaced the Server.Transfer with Response.Redirect, and all was back to normal.
I also put this answer on page loads twice due to js code.
For me it was solved by removing
Handles Me.Load
and changing the method like
Protected Overrides OnLoad(...)
For me, this issue cropped up suddenly after the Oct. 2017 Windows update. I noticed that for pages made accessible to anonymous users via a Location element in web.config, it is now necessary to also grant access to any assets referenced by that page, for example images, stylesheets, etc. The below example grants anonymous access to the login page and the 'images' directory (aka folder):
<location path="login.aspx">
<system.web>
<authorization>
<allow users="?" />
</authorization>
</system.web>
</location>
<location path="images">
<system.web>
<authorization>
<allow users="?" />
</authorization>
</system.web>
</location>
Update: I found a second cause of Page_Load being called twice. In old, legacy code, some pages' .aspx.designer.cs files contained inconsistencies that apparently hadn't caused problems until now. Instead of attempting repair, I created new pages, which eliminated the double load event.
I had same issue. I have set AutoEventWireup to false and after that remove the Page.Load += new EventHandler(Page_Load); from the InitializeComponent(). Now it's working.....
I think because of [DefaultEvent("Load")] in System.Web.UI.Page, the default event is Load so you don't need to add it again when your page class is initializing.
I am also experiencing page loading twice. For me, I have deduced that is happening only when aspx page contains <style> with internal css declaration. If I remove that tag, then it loads only once.
I have a situation where I need to ignore parts of page load sub inside a isPostback = true. Basically inside the isPostBack I want something like if button1 caused postback.... do this else do this...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = True Then
If TextBox1.Text <> String.Empty Then
CheckDate()
End If
End If
End Sub
I think what you need is the reference to the control name which triggered the postback.
http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx
Implement the solution which is there in the above link (Got it from here.... How to Get Source of postback)
If the control name is your button then do not do what needs to be done upon postback.
HTH
You should probably not have all this going on inside the Page_Load event. Instead, you should be handling events for each control that can cause a postback. This helps with code clarity, and ease of maintenance, not to mention better control in the first place.
Here's a nice brief blog entry I found on the subject: http://www.sitepoint.com/blogs/2007/01/21/page_load-is-evil/
Incidentally, handling events is much different in ASP.NET than in other environments, My guess, just based on the fact that you're trying to accomplish this in the Page_Load event is that you're not yet "getting" the event-driven programming model. (If I'm wrong, I apologize, I don't mean to be insulting).
If I'm right, however, once you get used to it, it's going to be a lot simpler for you than things were in the classic ASP days, for example, where you had to do things like try to figure out which button was clicked. Here's another nice article to explain this further: http://articles.sitepoint.com/article/driven-asp-net-development-c
It's hard to see this as a good idea. From the short snippet you posted, it looks like what you really need is a Validation control attached to your textbox.
Have a look at the POSTed items. You should see some sort of reference to that button in there. IIRC, if it was clicked, you will see some sort of reference in there, and if it wasn't it wouldn't be in there.
I had this same problem a while a ago and that's how I circumvented loading some stuff.
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" %>