Solved

BasePage OnLoad event fires twice when using master page

Posted on 2006-07-08
5
2,683 Views
Last Modified: 2007-12-19
I'm playing with asp.net 2.0 web site projects and I noticed something strange.
I have a default.aspx which uses MasterPage.master and BasePage.cs

Things seem to work correctly, but when I run the code in debug mode I can see that the OnLoad event of the BasePage class is called twice. Once before the Page_Load of Default.aspx and once before the Page_Load of the MasterPage.

Can anyone tell me if that's expected behaviour or am I doing something wrong?

Here's the relavent code:

"Default.aspx"
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Theme="Corp" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>

"Default.aspx.cs"
public partial class _Default : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}
-------------------------------
"BasePage.cs"
public class BasePage : Page
{
      protected override void OnLoad(EventArgs e)
      {
             base.OnLoad(e);
      }
}
-------------------------------
"MasterPage.master"
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

"MasterPage.master.cs"
public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
0
Comment
Question by:dddon123
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 17066146
hi,

i tested this - there might be something else initializing in the Default.aspx causing it...

this is what I did - bare with me -  test it with a new project

create project,

step 1 - add a master page (dont change it in any way)

step 2 - default.aspx  - PLEASE NOTE : Trace="true"
//////////////
<%@ Page Language="C#" AutoEventWireup="true" Trace="true"  CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/MasterPage.master" %>

<asp:Content ID="cs1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    hello
</asp:Content>

defualt.aspx.cs
////////////////
public partial class _Default : Class1
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

}

step 3 add a class: right click project add new item....
public class Class1 : Page
{
      public Class1()
      {
            //
            // TODO: Add constructor logic here
            //
      }

    protected override void OnLoad(EventArgs e)
    {
        Trace.Write("protected override void OnLoad(EventArgs e)");
        base.OnLoad(e);
    }

}


Doing this and then running the project the Trace.Write("protected override void OnLoad(EventArgs e)"); is only written once to trace detail at the bottom of the aspx page(under headingTrace Information),

thus I cannot answer your question as mine does what you probably expected with yours - but with using trace=true in your default.aspx page you might be able to answer it yourself.

good luck
0
 
LVL 1

Expert Comment

by:NEL_BusOps
ID: 17072295
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="test.aspx.vb" Inherits="Consumables.test"%>

At the top of your aspx page you should have something like the above minus 'AutoEventWireup="false"', add that and that will solve the problem, I did know what it was for but I have completely forgotten, a google should deliver the answers.
0
 
LVL 1

Accepted Solution

by:
NEL_BusOps earned 250 total points
ID: 17072362
Just seen you have AutoEventWireup, set it to false and you're good to go.
0
 
LVL 12

Assisted Solution

by:deanvanrooyen
deanvanrooyen earned 250 total points
ID: 17085061
i think nel solution will work and deserves the points,

if you want to understand why read below from msdn

Binding Page Events
ASP.NET pages raise life-cycle events such as Init, Load, PreRender, and others. By default, you can bind page events to methods using a naming convention of Page_eventname. For example, to create a handler for the page's Load event, you can create a method named Page_Load. At compilation time, ASP.NET will find methods based on this naming convention and automatically perform the binding between the event and the method. You can use the convention of Page_eventname for any event exposed by the Page class.

Note  
Page event-handling methods do not require any arguments.
 
If you prefer, you can create handlers explicitly. The automatic binding of page events based on the method naming convention is controlled by a page property named AutoEventWireup. By default, this property is set to true, and ASP.NET performs the automatic lookup and binding described earlier. Alternatively, you can set this property to false by adding the attribute AutoEventWireup=false in the @ Page directive. You can then create methods with any name and bind them to page events explicitly. In Visual Basic, you can use the Handles keyword....
0
 

Author Comment

by:dddon123
ID: 17127016
I appreciate your time, but setting AutoEventWireup to false was not the answer.
When I did, the Page_Load event of Default.aspx did not fire and the Page_Load and OnLoad events of the MasterPage and BasePage (Respectively) both fired twice.

Honestly, I don't know what the problem is.

I created a new web site and added the files in the same manner (same events, etc) and it works correctly. Everything fires once as expected.

I'm splitting the points between the two who responded to thank you for your effort.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question