p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: ASP.NET Website Programming Problem-Design-Solution (http://p2p.wrox.com/forumdisplay.php?f=23)
-   -   Adding Page_Load to base class doesn't work (http://p2p.wrox.com/showthread.php?t=18877)

Raeldor September 23rd, 2004 09:52 PM

Adding Page_Load to base class doesn't work
 
The code in the book says the base class should have...

protected override void OnInit(EventArgs e)
{
    this.OnInit(e);
    this.Load += new System.EventHandler(this.TestPage_Load);
}

and that this will get called BEFORE the inherited class page load event. However, auto-generated code in VS2003 shows the following...

override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
    this.Load += new System.EventHandler(this.Page_Load);
}

This means that the load even for the generated page will ALWAYS get called before our base class load event.

What gives? And what is the best way around this? I assume any chances to auto-gen code will get stamped over, plus it would be a major pain in the a$$ to fix this for every page.

Regards

Rael



Raeldor September 23rd, 2004 10:03 PM

Typo in first post, function should read...

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    this.Load += new System.EventHandler(this.TestPage_Load);
}

I was playing and forgot to set it back.


englere September 24th, 2004 06:30 AM

>I assume any chances to auto-gen code will get stamped over, plus it would
>be a major pain in the a$$ to fix this for every page

I change the auto-gen code in this case. It hasn't ever lost my changes. Some people say this change gets undone by VS, but I have never seen that myself.

It's not terribly hard to switch the order of 2 statements on every page. You're going to do a lot more work than that to each new page you write.

This all changes in ASP.NET 2.0. They have an improved model for handling identity and user personalization. The new master page concept also simplies this type of code by eliminating the user controls for things like the header and footer of each page. As I mentioned before, ThePhile in ASP.NET 2.0 could have a lot fewer lines of code, and still have more functionality.

However, this same code will still compile and run correctly under 2.0. It would require a fundamental redesign to get most of the big advantages of 2.0.

Eric


All times are GMT -4. The time now is 12:26 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.