[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

BasePage OnLoad event fires twice when using master page

Posted on 2006-07-08
5
Medium Priority
?
2,978 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

649 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