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.
Related
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.
I am using asp.net 4, .net 4 and masterpages. I added in the following code to my child page
Private Sub FoodChain_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
MyBase.OnInit(e)
PopulateDropdowns()
HideQuestionDiv()
HideDropdowns()
End Sub
and ran my app. The app runs fine but when I went back to the design view I now get an error saying "object reference not set to an instance". This is only during design time view and referencing the main place holder.
Is this a bug or am I missing something ? As I said the application runs fine and there are now issues during runtime.
Try putting all your databinding inside the following. (sorry about the c# code). I think it is Me.DesignMode in VB.
if (!this.DesignMode)
{
PopulateDropdowns();
HideQuestionDiv();
HideDropdowns();
}
I'm not sure if this works correctly for nested user controls but worth a try. Note that this won't work inside a constructor, only other events.
MSDN here.
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.
Working in .net 4.0, it still seems all my input controls have the attribute 'name', with a value that begins 'ct100$...'.
Is there any way to rename this?
I've gone all the way up the control hierarchy, and given each control an ID and set its clientidmode to 'Static' to no avail, even the 'earliest' controls on the page still inherit the prefix.
This is the master page's ID. I change it by adding a Page_Init to my masterpage which sets its id:
Private Sub InitSub(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
ID = "master"
End Sub
This ID is normally empty/null so when it renders it it generates an id (starting at ct100 and going up)
Like #Scott Stafford said, keep it short because it prefixes every client id on your page.
I use words like "mBio", "mHome", etc..
Why rename it? You can, as #Bob Fincheimer describes, but so what? Also, if you DO rename it, keep the new name short, because that name shows up in all generated HTML and all POSTing variables hundreds of times, possibly enough to actually affect performance of your site.
If you want to remove it, look in your web.config for the following tag:
<system.web>
...
<pages ... clientIDMode="*something*">
</pages>
...
</system.web>
Remove the clientIDMode="something" property specification. Just take it out.
** I stole my own answer from here.
In ASP.Net, is anyone aware of a way to bypass Forms Authentication if a specific query string parameter is passed in?
Such as:
mydomain.com/myprotectedpage.aspx
...I would like to be protected by Forms Authentication (and so, redirected to login page)
mydomain.com/myprotectedpage.aspx?myBypassParameter=me
...I would like the page to render as normal
Is this at all possible?
Not really any "official" way of doing it.
You could do what I do, is have a base page instead of system.web.ui.page like so:
Public MustInherit Class ProtectedPage
Inherits System.Web.UI.Page
Private Sub Page_InitComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.InitComplete
If User.Identity.IsAuthenticated = False Then
If String.IsNullOrEmpty(Request.QueryString("myBypassParameter")) Then
FormsAuthentication.RedirectToLoginPage()
End If
End If
End Sub
End Class
In your code behind, you could simply use Request.QueryString["myBypassParameter"] and check its value. If it's an invalid value, then use FormsAuthentication.RedirectToLoginPage or a custom redirect to put the user back at the log in page. However, this doesn't seem like a secure method of protecting a page. What if someone got hold of the specific parameter and managed to gain access to your protected page? Also, you want to make sure that the QueryString value is valid (maybe by a regular expression) to ensure the user hasn't passed malicious code which will then be read by your application.
You might be able to jam some quick code into the Application_AuthenticateRequest event. You could then test for the parameter and adjust the User.Identity as necessary to allow the page. You'd have to put in a page check as well to make sure it didn't allow this behavior on all restricted pages.
I wouldn't recommend this design as an approach though. If you need to have a protected area accessed in an anonymous fashion, it'd be better to put all of your functionality into a UserControl and then use a protected/unprotected version of a parent page. This would allow you to control what goes out and when.