?
Solved

BasePage OnLoad event fires twice when using master page

Posted on 2006-07-08
5
Medium Priority
?
3,053 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 500 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 500 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

862 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