ASP.NET Tip: Selecting a Master Page at Runtime

Master pages, introduced with ASP.NET 2.0, provide developers an easy way to create a shared layout for a site or portion of a site without having to work around Visual Studio .NET. While many sites will have a master page specified in each page by default, you can also design your site to select a master page dynamically. For example, an application I've created contains two master pages. Which one the user sees depends on which hostname he or she uses to visit the site. I do this so that the graphics and text are appropriate for the domain, but I have a single Web site servicing both domains behind the scenes.

The trick to selecting a master page at runtime is to add code into the OnPreInit event handler of your Web page. This is the point at which you can select the master page to be used for the particular Web page. For instance, you might have code that looks like this:

protected override void OnPreInit(EventArgs e)
{
   base.OnPreInit(e);
   if (Request.Url.Host.Contains("test.com"))
      base.MasterPageFile = Server.MapPath("test.com.master");
   else
      base.MasterPageFile = Server.MapPath("test2.com.master");
}

The MasterPageFile property requires a pathname and not a URL reference. However, you can always use Server.MapPath to convert your URL into an actual filename.

This feature works quite well for the content management system I've built. Use it to allow a page to choose a different look on the fly, based on whatever criteria you choose.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at questions@techniquescentral.com.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds