`
javatoyou
  • 浏览: 1016510 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

vs.net 2008 支持嵌套母版页,有时间就翻译一下

 
阅读更多

"Master Page"support was one of the most popular featuresintroduced with ASP.NET 2.0, and is one of those features that almost every new ASP.NET project now uses to deliver consistent layout templatesupport across a web site.

One of the cooler scenarios that ASP.NET 2.0 supports is the ability to have "nested master pages" - where you can create a root master page for a site that defines a site's overall layout, and then create nested master pages that are based on the root master and further customize the layout (for example: you could create a SingleColumn.Master and TwoColumn.Master that defined 1 or 2 column layout structures based on the root master template). This nested master page feature is extremely flexible, and enables developers and designers to quickly and very cleanly make changes to the layout and organization of a site with minimal code changes and no content duplication. The one (big) catch, though, is that Visual Studio 2005 doesn't really support using nested master pages, and pages based on nested masters can't be edited within the VS 2005 WYSIWYG designer.

The good news is that VS 2008 fully supports nested master pages, and makes using them super easy. Going forward I recommend that almost all ASP.NET projects should advantage of this feature - since it can add tremendous flexibility to the UI of your projects.

Using Nested Master Pages With VS 2008

One of the sites I recommend checking out is http://www.opensourcetemplates.org/. This is an online repository of free HTML site templates that you can browse, download and use. The templates on the site are pure HTML (meaning you can use them with any server-side programming technology), and are built using clean CSS and XHTML markup:

To help with this blog post, I picked one of the templates that I thought looked nice. You can browse it online here, and download it here.

Like most web-sites out there, this template has pages that utilize a few different layout approaches for displaying content. For example, it includes a page that uses multiple-columns to layout content:

It also has pages that use a single column approach that fills the entire width of the page with content:

Using the Above HTML/CSS Template with Nested Master Pages

Converting the above template to use ASP.NET and its nested Master Page support is really easy with VS 2008.

Step 1: Create a Site.Master template

To begin with we can create a new root master page file that we'll use to define the overall layout and structure for all pages on the site. We'll name this file "Site.Master" and copy/paste the "outer chrome" HTML from the template we downloaded into it. We'll then add a <asp:contentplaceholder> into the content section in the middle where we'll fill in page specific content. We'll name this <asp:contentplaceholder> control "MainContent":

Step 2: Create SingleColumn.Master Template

We used our Site.Master template above to define the "outer chrome template" of the site we are working on. We'll now want to create a few nested master pages that provide further templates that customize the layout of the "MainContent" section.

To do this we can right-click in the solution explorer and choose the "Add New Item" menu option. We'll create a new Master Page called "SingleColumn.Master" and will want to make sure that the checkbox indicating that we want to base this master page offa parent master pageis selected:

VS 2008 will then allow us to pick the Site.Master templateas the master page we want to base this new SingleColumn.master template on:

VS 2008 will then create a new SingleColumn.master file that looks like below:

Notice above how VS 2008 has picked up the fact that we defined a <asp:contentplaceholder> control in the root Site.Master file called "MainContent" - and automatically added a blank <asp:content> control within this nested master page so that we can override and customize that content region.

Also notice above how we can use the new "Split View" support within VS 2008 to enable us to see both the HTML source and WYSIWYG designer surface at the same time. We can make changes within either of the split views, and immediately see the changes applied in the other (hint: make sure to show your boss this new feature when asking for a bigger monitor).

Our SingleColumn.Master template is going to end up being really simple to begin with, and will just add a CSS rule to define the width of the single column of content - and then add its own <asp:contentplaceholder> control so that pages based off of this master can fill in their page specific content:

Step 3: Create TwoColumn.Master Template

In addition to having our SingleColumn.Master template, we'll want to create a second nested master page template to handle two columns of content. We'll repeat the steps we followed above - but this time call the file "TwoColumn.Master". We'll define the layout of this file like below:

Notice above how we have added two <asp:contentplaceholder> controls within this nested master page - one called "MainColumn" (just like the SingleColumn.Master), and the other called "LeftColumn" (to handle a smaller column of content that will be displayed on the left of the page). We'll use standard HTML and CSS to position these two columns.

Step 4: Creating Content Pages using our Nested Master Pages

Now that we've defined master pages to control our site's layout structure, we can start adding pages to the site.

Let's create a new page that we'll call "HomePage.aspx" - and indicate that we want it to be based on a master page:

We can then choose which master page or nested master page we want to base it on. For the home-page let's use the two column layout:

VS 2008 will then base HomePage.aspx on this nested master file, and add two empty <asp:content> controls to the page that allows us to fill in the left and main column content for the page:

Notice above how we not only get WYSIWYG support for pages built using nested master pages, but we can also use split-view with them.

I can then fill in the specific content I want into the appropriate<asp:content> regions, and add any code I want to the code-behind:

I canlateradd additional pages to the project, and base them on either the SingleColumn.Master or TwoColumn.Master template.

Because both of these templates are based on a root Site.Master template, if I make any changes to this root template (for example: I want to change the top logo or navigation structure), it will automatically apply to all pages within the site without me having to modify them at all.

I also now have the flexibility to change the layout of the SingleColumn or TwoColumn templates (for example: adjusting their width) andall of the pages on the site that are built with the modified master templates will immediately have the changes applied.

Summary

VS 2008 has full support for using ASP.NET nested master pages (this feature is also fully supported with the free Visual Web Developer Express 2008 edition). I think you'll find that this makes building consistent UI and site layout much easier.

Best of all, because VS 2008 now has multi-targeting support you'll be able to use the above feature immediately without having to install .NET 3.5 on your servers. You can open existing ASP.NET 2.0 projects in VS 2008, have VS 2008 continue to target .NET 2.0 as the runtime target, and start using this feature.

Hope this helps,

Scott

分享到:
评论

相关推荐

    VS2008(C#)中的ASP.NET子页嵌套多层母版页的控件访问方法

    VS2008(C#)中的ASP.NET子页嵌套多层母版页的控件访问方法

    asp.net 嵌套母版页(实例演示)

    asp.net 嵌套母版页(实例演示) 包含有母版页、嵌套母版页、2个内容页可以直观的看到嵌套母版页应用的效果。

    ASP.Net巧用窗体母版页实例

    主要介绍了ASP.Net巧用窗体母版页的方法,以实例形式详细分析了母版页的用途及嵌套用法,具有一定的学习借鉴价值,需要的朋友可以参考下

    使用模板页后内容页中控件查找

    页面使用母版页后,在页面中查找指定的控件是否存在,以及得到的控件类型是否和自己控件的类型一致;从而判断是否可以显示

    asp.net母版页如何使用

    继上篇文章,本文与大家分享asp.net母版页如何使用的全过程,并附有步骤截图,感兴趣的小伙伴们可以参考一下

    ASP.NET开发实战1200例(第Ⅰ卷)第十章

    第10章 母版页技术 420 10.1 母版页基础 421 实例261 创建.master母版页文件 421 实例262 添加内容页 423 ...实例271 嵌套母版页 434 实例272 高速缓存母版页 436 实例273 ASP.NET AJAX和母版页应用 437

    ASP.NET 页面控件遍历

    ASP.NET 页面控件遍历(非嵌套母版页的情况下)

    《零基础学ASP.NET 2.0》第9章 使用母版页统一网站界面

    9.1 母版页概述 162 9.2 母版页的创建 162 9.3 母版页的使用 164 9.3.1 在内容页中使用母版页 164 ...9.3.3 母版页的嵌套 167 9.4 常见问题 169 9.4.1 母版页是否可以动态切换 169 9.4.2 母版页使用频繁吗 170

    精通ASP.NET.4.0网络编程 part-2

    第1篇ASP.NET网络开发关键技术第1章ASP.NET网络开发基础(教学视频87分钟)1.1ASP.NET事件模型和页面生命周期1.1.1经典的Web事件处理方法1.1.2ASP.NET服务器控件事件模型1.1.3ASP.NET页面生命周期1.2母版页1.2.1母版页...

    精通ASP.NET.4.0网络编程 part-1

    第1篇ASP.NET网络开发关键技术第1章ASP.NET网络开发基础(教学视频87分钟)1.1ASP.NET事件模型和页面生命周期1.1.1经典的Web事件处理方法1.1.2ASP.NET服务器控件事件模型1.1.3ASP.NET页面生命周期1.2母版页1.2.1母版页...

    C#(ASP.NET)相关技术

    网站采用.net 2.0技术 ...母版页(母版页嵌套) 上一则下一则 树形控件 数据导出 数据库备份还原操作 数据相关操作 图片广告 委托与事件 文件操作 站点地图 流水号生成 -------------------详见附件(网站)

    ASP.NET 控件的使用

    5.1.2 嵌套母版页 166 5.1.3 在母版页中使用图片和超链接 170 5.1.4 在Web配置文件中注册母版页 172 5.2 修改母版页内容 172 5.2.1 使用Title属性 173 5.2.2 使用Page Header属性 173 5.2.3 暴露母版页属性 175 ...

    控件嵌套在ajax的模板中 需要提示的对话框写法

    如果网站页面潜逃了ajax 想在后台利用js弹出对话框 试试这个方法 挺好用的

    Asp.net三层框架

    asp.net项目三层框架样式表、嵌套母版页、LinqDataSource、EntityDataSource、ListView以及DataPager等控件的使用;详细说明了C#2.0以及C#3.0中的新特性,如泛型、扩展方法、Lambda表达式等,并对最新的ASP.NETMVC...

    ASP.NET.4揭秘

    5.1.2 嵌套母版页186 5.1.3 在母版页中使用图片和超链接191 5.1.4 在web配置文件中注册母版页193 5.2 修改母版页内容194 5.2.1 使用title属性194 5.2.2 使用page.header属性194 5.2.3 暴露母版页属性196 5.2.4 对...

    asp.net程序设计使用教程课件ppt

    6.3 母版页嵌套和动态加载 7.1 主题和样式 7.2 个性化 8.1 部署、测试、IIS配置 8.2 调试 9.1 SqlDataSource数据源的使用 9.2 ObjectDataSource数据源的使用 9.3 数据绑定的基本知识 9.4 DataList控件的常用属性、...

    ASP.NET 3.5 开发大全

    12.2.4 嵌套母版页 12.3 Microsoft Expression 2 12.3.1 Microsoft Expression 2简介 12.3.2 安装Microsoft Expression 2 12.4 使用Microsoft Expression Web 2制作页面 12.4.1 创建ASPX页面 12.4.2 创建CSS层叠样式...

    ASP.NET3.5从入门到精通

    12.2.4 嵌套母版页 12.3 Microsoft Expression 2 12.3.1 Microsoft Expression 2 简介 12.3.2 安装Microsoft Expression 2 12.4 使用Microsoft Expression Web 2 制作页面 12.4.1 创建ASPX 页面 12.4.2 创建CSS 层叠...

    ASP.NET 3.5 开发大全word课件

    12.2.4 嵌套母版页 12.3 Microsoft Expression 2 12.3.1 Microsoft Expression 2简介 12.3.2 安装Microsoft Expression 2 12.4 使用Microsoft Expression Web 2制作页面 12.4.1 创建ASPX页面 12.4.2 创建CSS层叠样式...

Global site tag (gtag.js) - Google Analytics