Page_Load gets called twice


F

fiefie.niles

I am using ASP.NET 2005 and I have a simple form. Page_Load calls a
sub mySub that does not do anything (for testing purposes). But,
Page_Load gets called twice.
On every single ASPX page in my site, Page_Load gets called twice. Why
is that and how can I fix this problem ?
Thank you very much.

Imports System.IO
Namespace myProject
Partial Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents HyperLink1 As
System.Web.UI.WebControls.HyperLink

#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region

Protected Sub Page_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load, Me.Load
mySub()
End Sub
Sub mySub()
End Sub
End Class
End Namespace
 
Ad

Advertisements

J

Juan T. Llibre

Are you setting AutoEventWireup to true
(or are you not defining it, since by default it is true) ?

Page events are automatically bound to methods that use the naming convention of
Page_event, such as Page_Load, when you set AutoEventWireup to true or not set it to false.

If you set AutoEventWireup to true, and then use the Handles statement,
you will get Page events called twice, once by AutoEventWireup,
and again when you use Handles.

Test by setting AutoEventWireup to false, or by eliminating the Handles statement.



Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
 
F

fiefie.niles

Thank you for your reply.
Are you setting AutoEventWireup to true ?
Do I find this in the myPage.aspx ?
This is what it says in myPage.aspx:
<%@ Page Language="vb" AutoEventWireup="false"
Inherits="Auction.WebForm1" CodeFile="myPage.aspx.vb" %>
I do not define/change it, it is like that already. Does it mean that
it is set to False ?
Test by setting AutoEventWireup to false, or by eliminating the Handles statement.
How can I eliminate the Handles statement ?

Thank you.
 
J

Juan T. Llibre

re:
!> This is what it says in myPage.aspx:
!> <%@ Page Language="vb" AutoEventWireup="false"
!> Does it mean that it is set to False ?

Yes, it is set to false.

re:
How can I eliminate the Handles statement ?

Since you already have AutoEventWireup set to false, all you have to do is eliminate
the double-loading of the code base. i.e., you currently have this :

Protected Sub Page_Load(...) Handles MyBase.Load, Me.Load

That loads the code twice ( once with MyBase.Load and again with Me.Load )
In ASP.NET 1.1 you could get away with that, but 2.0 is a bit stricter.

Use :
Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load

If you set AutoEventWireup to true, you wouldn't need the Handles method, and you could use :

Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
Thank you for your reply.
Are you setting AutoEventWireup to true ?
Do I find this in the myPage.aspx ?
This is what it says in myPage.aspx:
<%@ Page Language="vb" AutoEventWireup="false"
Inherits="Auction.WebForm1" CodeFile="myPage.aspx.vb" %>
I do not define/change it, it is like that already. Does it mean that
it is set to False ?
Test by setting AutoEventWireup to false, or by eliminating the Handles statement.
How can I eliminate the Handles statement ?

Thank you.
 
F

fiefie.niles

Thank you very much for your help.
Your suggestion to eliminate Me.Load from the Page_Load works !
Thanks a lot !

So, I have to do this on every page ?
Do you know why ASP.Net 2005 default both Me.Load and MyBase.Load ?
Is there any need to call up Page_Load twice ?

THanks again for your help.
 
F

fiefie.niles

One more info: these pages were converted from ASP.NET 2003 to ASP.NET
2005 using the wizard in Visual Studio 2005. Do you think that's why
all my pages have both MyBase.Load and Me.Load in the Page_Load ?
Thanks
 
Ad

Advertisements

J

Juan T. Llibre

re:
!> these pages were converted from ASP.NET 2003 to ASP.NET
!> 2005 using the wizard in Visual Studio 2005. Do you think that's why
!> all my pages have both MyBase.Load and Me.Load in the Page_Load ?

Short answer : yes.





Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
One more info: these pages were converted from ASP.NET 2003 to ASP.NET
2005 using the wizard in Visual Studio 2005. Do you think that's why
all my pages have both MyBase.Load and Me.Load in the Page_Load ?
Thanks
 
F

fiefie.niles

Thanks again.
Do you know if there is ever any need to call up Page_Load twice ?
 
J

John Berberich

re:
!> This is what it says in myPage.aspx:
!> <%@ Page Language="vb" AutoEventWireup="false"
!> Does it mean that it is set to False ?

Yes, it is set to false.

re:


Since you already have AutoEventWireup set to false, all you have to do is eliminate
the double-loading of the code base. i.e., you currently have this :

Protected Sub Page_Load(...) Handles MyBase.Load, Me.Load

That loads the code twice ( once with MyBase.Load and again with Me.Load )
In ASP.NET 1.1 you could get away with that, but 2.0 is a bit stricter.

Use :
Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load


Juan,
I understand that the problem that you pointed out, but I'm curious
why would have Page_Load handle MyBase.Load as opposed to Me.Load?

Thanks,
John
 
Ad

Advertisements

J

Juan T. Llibre

re:
!>why would have Page_Load handle MyBase.Load as opposed to Me.Load?

Hi, John.

Please read through this thread:
http://www.dotnet247.com/247reference/msgs/6/34488.aspx

It's an excellent discussion which will clarify the concepts.

Basically, only the base class has the Load event (in this case)
but the derived class *can* handle this event if it desires.

That's why the Handles is MyBase.Load rather than Me.Load.

Since Me refers to the derived class, it doesn't have
any events except the ones that it explicity creates.

You *could* choose to use Me.Load instead of MyBase.Load,
but you'd have to be careful and override any specific events wanted.

This doesn't appear to make much sense in inline code, but in code-behind,
and if you're deriving a custom class from the Page class, it could be critical.

Me ? I take the easy way out. I use AutoEventWireup="true" and inline code.

Look, ma, no Handles ( and no "Me" ) !

:)






Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
re:
!> This is what it says in myPage.aspx:
!> <%@ Page Language="vb" AutoEventWireup="false"
!> Does it mean that it is set to False ?

Yes, it is set to false.

re:


Since you already have AutoEventWireup set to false, all you have to do is eliminate
the double-loading of the code base. i.e., you currently have this :

Protected Sub Page_Load(...) Handles MyBase.Load, Me.Load

That loads the code twice ( once with MyBase.Load and again with Me.Load )
In ASP.NET 1.1 you could get away with that, but 2.0 is a bit stricter.

Use :
Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load


Juan,
I understand that the problem that you pointed out, but I'm curious
why would have Page_Load handle MyBase.Load as opposed to Me.Load?

Thanks,
John
 
Ad

Advertisements

P

prasanth k

how can i solve this problem using C#.net
because in C# there is no handles clause



Juan T. Llibre wrote:

Re: Page_Load gets called twice
01-May-07

re
!>why would have Page_Load handle MyBase.Load as opposed to Me.Load

Hi, John

Please read through this thread
http://www.dotnet247.com/247reference/msgs/6/34488.asp

It's an excellent discussion which will clarify the concepts

Basically, only the base class has the Load event (in this case
but the derived class *can* handle this event if it desires

That's why the Handles is MyBase.Load rather than Me.Load

Since Me refers to the derived class, it doesn't hav
any events except the ones that it explicity creates

You *could* choose to use Me.Load instead of MyBase.Load
but you'd have to be careful and override any specific events wanted

This doesn't appear to make much sense in inline code, but in code-behind
and if you're deriving a custom class from the Page class, it could be critical

Me ? I take the easy way out. I use AutoEventWireup="true" and inline code

Look, ma, no Handles ( and no "Me" )




Juan T. Llibre, asp.net MV
asp.net faq : http://asp.net.do/faq
foros de asp.net, en espa?ol : http://asp.net.do/foros
==================================
On Apr 30, 3:59 am, "Juan T. Llibre" <[email protected]
wrote

Juan
I understand that the problem that you pointed out, but I'm curiou
why would have Page_Load handle MyBase.Load as opposed to Me.Load

Thanks
John

EggHeadCafe - Software Developer Portal of Choice
Repository Factory, GridView and ObjectDataSource with WebService Layer
http://www.eggheadcafe.com/tutorial...d0-a7a2991a79d4/repository-factory-gridv.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top