View Single Post
  #4 (permalink)  
Old March 5th, 2009, 06:39 PM
Imar's Avatar
Imar Imar is offline
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

You don't inherit a "namespacehere", you inherit a "classnamehere" Although this sounds like nitpicking, it's an important difference.

Your ASPX pages have the behavior they have because they inherit from Page. The ASP.NET supplies a Page class with page-like behavior. As soon as you inherit from such a class, your page behaves like a Page as well.

Similarly, a MasterPage inherits from MasterPage which gives it MasterPage like behavior.

Consider a an example like Animal versus Cow. A Cow inherits from Animal which gives it animal like behavior. A Car inherits from Vehicle which gives it vehicle like behavior. You can't make a Cow inherit from Vehicle and give it wheels (although they seen to do that many years ago)

When you inherit from a BasePage, you introduce a new in-between later. So:

  + YourPage

  + BasePage
    + YourPage
Likewise, you could have a new hierarchy for Master Pages:

  + BaseMasterPage
    + YourMasterPage
In order to do this, simple create a class similar to BasePage, call it BaseMasterPage (or whatever you see fit), let that class inherit MasterPage and make your master pages inherit your new BaseMasterPage class.

thinking about the logic that you would put in the base page could you give me an example?
The BasePage in the Planet Wrox site has a good example: checking the title, although there are many more applications for a BasePage. Stuff like globally assigning titles, keywords, descriptions, security checks etc come to mind.

And you say the master page can't inherit from the Base Page but is that just me using the terms wrong? eg:

Could the masterpage.master file not have an inherits "namespacehere" ?
No, it can't. MasterPage ultimately must inherit MasterPage, while pages must inherit Page somehow. An intermediate class (or more) is fine, as long as somehow some class inherits Page.
Or do you put that code in the codebehind files of the individual pages ?
Not sure what you mean by this. Code can be put in code behind files of Pages and MasterPages and in "code classes" like BasePage.

In any case do you mean server side code runs on the basepage and client side code would be put in a master page or normal page?
No, not at all. You can put server side code in Pages, MasterPages and in code classes. What I said was that stuff like Google tracking code is markup and as such belongs, IMO, in markup capable pages like those with an .ascx, aspx and .master extension. But again: it's a matter of preference. You could, as I said in my earlier post, generate client side code from server side code in a class Like BasePage. Not pretty, but it would work. If I weren't using MasterPages, I would put the Google code in all of my .aspx pages, or maybe I would create it programmatically in BasePage. However, since you're using MasterPages, they would be a perfect candidate for storing scripts like that.

Does this clarify things?

Imar Spaanjaars
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; March 5th, 2009 at 06:42 PM..
Reply With Quote