Solved

BasePage OnLoad event fires twice when using master page

Posted on 2006-07-08
5
2,634 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
Comment Utility
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
Comment Utility
<%@ 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now