I have a ASP.Net 4.0 web application whose pages have a common layout (top navigation bar, left navigation bar, logo etc…).
Masterpages would be perfect for the layout part, and this is what I am planning to use. I am not used to working with Masterpage, so many of my plans are still to be tested.
However, I also want my pages to have some common functionality which should be performed before I render the HTML of the page. This functionality includes checking whether the user has access to that page. Moreover that common functionality differs slightly from page to page. Some pages will not even need it.
You would think it was obvious to include this functionality in the Masterpage, and have the functionality depend on which content page is requested.
However it has to be put before the content pages page_load fires, since its result has influence on rendering the page.
Is there a best practice as where to put this functionality, knowing that we need information from the content page to parametrize the functionality.
Ideally that would be in the OnInit() of the Masterpage, since this is run before all. But I do not believe I can retrieve the necessary information from the content page at this time.
Thanks a lot
Jihad
The MasterPage is not the appropriate place for this functionality. The best place for this would be in a custom base Page class where your special functionality is in your derived Page. That way you can inherit your custom class and call the functionality that you need on a page by page basis.
Then when you create an individual page in the site, you simply switch its inheritance from
System.Web.UI.Pageto your own class.