ARTICLE

Dynamically Loading Master Pages in ASP.Net: Part 2

Posted by Abhimanyu Kumar Vatsa Articles | ASP.NET using VB.NET August 13, 2010
In this article we will discuss how to load Master Pages dynamically for multiple content pages; it has great advantage in new web technologies, please do read completely. Please do read previous part for complete understanding.
 
Reader Level:

Introduction

 

In Part 1 of this article, we have discussed how to load Master Pages Dynamically for one content page but in this part we will discuss how to load for multiple content pages even. Please do read previous part then come to this otherwise you can't understand completely. The easiest way to apply the same logic to multiple content pages is to create a new base Page class. The example given below contains a new Base Page class named DynamicMasterPage. Now we have to add the file given below in our application's App_Code folder. If you create class file at very first time then it will prompt you to create App_Code Folder to place that class file.

 

DynamicMasterPage.vb Class File Code

 
Imports Microsoft.VisualBasic

Public Class DynamicMasterPage
    Inherits Page

    Protected Overrides Sub OnPreInit(ByVal e As EventArgs)
        Me.MasterPageFile = CType(Context.Profile("MasterPageFile"), String)
        MyBase.OnPreInit(e)
    End Sub

End Class

 

Web.config File Code

 
<?xml version="1.0"?>
<configuration>
                <
system.web>
                                <
compilation debug="true" targetFramework="4.0"/>
                </system.web>

  <system.web>

    <pages pageBaseType="DynamicMasterPage" />

    <profile>
      <
properties>
        <
add
          name="MasterPageFile"
          defaultValue="DynamicMaster1.master" />
      </properties>
    </
profile>
  </
system.web>

</configuration>

 

If you register the DynamicMasterPage class as the base Page class, every page in our application automatically inherits from the new base class. Every page inherits the new OnPreInit() method and every page loads a Master Page dynamically. This is pretty cool and used on maximum websites today.

 

Note: This is last part of this article series. 

HAVE A GREAT CODING!

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor