ASP.NET Forums
首页 搜索 用户列表 FAQ 注册 登录  
ASP.NET Forums » .Net 专区 » 探索Visual Studio » Dynamically Loading Master Pages in ASP.NET 2.0
  Dynamically Loading Master Pages in ASP.NET 2.0
帖子发起人: venjiang   发起时间: 2006-7-7 3:29 AM   回复数: 0
« 上一主题 下一主题 »
楼主
  2006-7-7, 3:29 AM
venjiang 离线,最后访问时间: 12/2/2008 9:34:33 PM venjiang



发帖数前10位

超级管理员
职务: 超级管理员
参谋长
等级: 参谋长
注册: 2004年6月8日
区域: 中国河北
积分: 2,108
精华: 9
发贴: 1,497

ASP NET Forums2.0 中文版开发团队
Dynamically Loading Master Pages in ASP.NET 2.0
 

One of the cool new things introduced in ASP.NET 2.0 is Master Pages. Master Pages give you the ability to define a master page layout and look that is used throughout a site to give a consistent look & feel to all pages. Any updates or changes to the look & feel of the site is done in only one place - the Master Page. Something that might be useful is the ability to dynamically load a master page at runtime, for example based on user preferences or to implement a simple skinning ability for a site framework that might host multiple different sites (something like .Text). It is not a difficult task and can be done very generically to make for easy reuse.

The master page can be selected for a page easily at design-time, I won't focus on that here. But assigning a master page at run-time can only be done in the OnPreInit event since the rendering of the page with the master page occurs prior to the Init event. However, in the case where you want to load the master page dynamically, for example from a database entry based on a user's preference or something, you don't want to add code to the OnPreInit event on every page in the site. It is easy enough to add that code to a base page class that you can inherit from for you pages in the site.

public class DynamicPage : System.Web.UI.Page { protected override void OnPreInit(EventArgs e) { string masterfile = getMasterPageFromDatabase(); if (!masterfile.Equals(string.Empty)) { base.MasterPageFile = masterfile; } base.OnPreInit(e); } }

Now, all you have to do is inherit from that class for all the pages in the site. But this is where it gets even cooler. I posted before about some cool changes for the web.config file in ASP.NET 2.0. So now, you don't even have to change each page class in your web application to inherit from our new DynamicPage class. You can just add it into the web.config and all pages will automatically inherit from it. Doesn't matter if it is a page in the project that you have already, or a new page you add later, it will know to inherit from the DynamicPage class. You add the following to the web.config:

<system.web> <pages pageBaseType="DynamicPage" /> <!-- ...rest of system.web items... --> </system.web>

Make sure to include the fully-qualified name, including namespace(s) if appropriate, but that is it. I did a quick test and threw a couple of master pages together to test it out. I also added a property to my DynamicPage class to allow for setting the master page file at runtime from a page that inherits from the class and I added a dropdown to allow the master page to be selected by the user. The property I added just stores the master page file name and reloads the current page so the PreInit would fire again, nothing too fancy. Here's some quick images of the pages with the different master pages applied to it (as selected by the user at run-time)

       

Gives a quasi-skinning feel to the site, none of the content pages need to change - and took me all of about 5 minutes to do. All you need to do is implement separate master pages for each look - which is brain-dead easy. I didn't make my master pages anything fancy at all, but changed enough to demonstrate a different look and layout for each master. Don't forget that this does not replace other good design habits, you can still implement the parts that make up the master page as user controls so you're not building things more than once. You can use this same idea with ASP.NET 2.0 themes to load themes for controls at run-time also.


IP 地址: 已登录   来自: 已登录    返回顶部
 第 1 页 总共 1 页 [共有 1 条记录]
ASP.NET Forums » .Net 专区 » 探索Visual Studio » Dynamically Loading Master Pages in ASP.NET 2.0

友情链接: hiDotNet官方论坛 | hiDotNet知识库 | 其它友情链接

Asp.Net Forums version: 2.5.2725
(C)Copyright 2004-2007, hiDotNet.com. All Rights Reserved.
意见反馈 | 关于我们

Powered by Community Server :: Forums 中文本地化: hiDotNet.com