ASP.NET 2.0: custom base Page class (derived from System.Web.UI.Page)

H

hamstak

I had developed a custom base page class for the 1.1 framework which I
have now migrated to the 2.0 framework. I discovered that the new
partial class system -- which appears to "automagically" assign the
Page_Load method as the Load event handler in the hidden partial class
-- does not carry over to derived Page classes. This being the case,
and not wanting to copy what used to be the VS generated code into
every derived page, I decided to attempt to assign the Load event
handler in the custom base class.

This is what I came up with:


/// Custom base class ////////////////////////////////////////

protected class BasePage : System.Web.UI.Page
{

// ctor, other code implied

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

virtual protected void Page_Load
{
// no code defined
}

} // end class


/// Derived class /////////////////////////////////////////

public class MyPage : BasePage
{

// ctor, other code implied

override protected Page_Load(object sender, EventArgs e)
{
// some code
base.PageLoad(sender,e);
}

} // end class

//////////////////////////////////////////////////////////////

This seems to work like a charm. If anyone has any comments on why
this is a less-than-preferable technique, suggestions for improvement,
or a more elegant solution I would be glad to hear them!

Tom
Mnemoweb Incorporated
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top