ASP.NET Tip: Selecting a Master Page at Runtime San Jose CA

Enable your Web application to select a master page dynamically at runtime based on whatever criteria you choose.

Local Companies

cccp
408-265-2902
2585 Westgate Ave
San Jose, CA
Accoladde
+91 9962299053
2033 Gateway place
San Jose, AK
Accoladde
+1 408 651 7050
2033 Gateway Place
San Jose, CA
Concierge for Business
408 993-1368
472 Clifton Avenue
San Jose, CA
VeriPIc
1-888-837-4742
2360 Walsh Ave
Santa Clara, CA
Zoniac, Inc.
619.448.7284
1649 S. Main Street, Ste. 105
Milpitas, CA
InsidersReferral.com, Inc.
(408) 338-6542
386 America Ave
Sunnyvale, CA
Accept Software Corporation
1.866.423.8376
42808 Christy Street, Suite 216
Fremont, CA
smartData Enterprises
646-367-8215
706 Colorado Avenue,
Palo Alto, CA 94303, CA
Oshaana Creative
(408) 280-1133
28 N 1st St
San Jose, CA

ASP.NET Tip: Selecting a Master Page at Runtime

provided by: 
Originally published at Internet.com


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.

Author: Eric Smith

Read article at Internet.com site

Featured Local Company

cccp

408-265-2902
2585 Westgate Ave
San Jose, CA
http://cccptech.com

Related Local Events
Semicon West
Dates: 7/14/2009 - 7/16/2009
Location: Moscone Convention Center
San Francisco, CA
View Details

Target Discovery World Congress
Dates: 8/4/2009 - 8/5/2009
Location: The South San Francisco Conference Center
San Francisco, CA
View Details

IT Security World Conference & Expo 2009
Dates: 8/5/2009 - 8/7/2009
Location: San Francisco Marriott
San Francisco, CA
View Details

Lab-on-a-Chip World Congress
Dates: 8/6/2009 - 8/7/2009
Location: The South San Francisco Conference Center
San Francisco, CA
View Details

NGDC - NEXT GENERATION DATA CENTER 2009
Dates: 8/10/2009 - 8/13/2009
Location: Moscone Convention Center
San Francisco, CA
View Details
Rate Article
     
Articles Insider

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Advertising Entertainment Home Services Retail & Consumer Services
Business Services Environmental Industrial Goods & Services Software
Career Family Insurance Technology
Cars Fashion Internet Telecommunications
Computer Hardware Financial Services Legal Transportation & Logistics
Construction Food & Beverage Pets Travel
Education Health Real Estate Wedding
Engineering Home Electronics