Solved

BasePage OnLoad event fires twice when using master page

Posted on 2006-07-08
5
2,712 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

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