ASP.net 2.0 Migrating系列 – Master Pages 感觸

來源:互聯網
上載者:User

ASP.net 2.0 Migrating系列 - Master Pages
範維肖

在Visual Web Developer 2005裡的建立裡多了一個Master Pages,在微軟的VWD2005的Guided Tour裡有專門對它使用的介紹,可見它是ASP.NET2.0裡的一大特色.
最早我們在做HTML頁面的時候,有時會有遇到這樣的情況:一個網站要有一個統一的風格,所以頁面的樣式是大體一致的,因此我們都喜歡做一個包括導覽列在內的Model頁面,然後每次都開啟它,然後另存新檔像aboutus.html這樣的頁面,然後再編輯,再儲存。後來東西多了,我們就開始用Frameset把頁面分開,像導覽功能表,著作權聲名部分都用專門的frame。到了ASP時代,通常也是把版面設計和asp代碼分開。設計完版面再填寫代碼,對於有相似樣式的頁面,我們也是通過copy樣式的html代碼來實現的,後來為了適應skin技術,有人開始做一個model頁面,裡面需要插入ASP的部分用類似$BlogContent$的標記來表示,然後用程式再去分析。到了ASP.NET的時代,有時我們也嘗試著用userControl來實現frameset那樣的思想。這些我們的“土方法”最終在VWD2005結束了,這個新的feature相比是VWD2005的設計者也是飽受了痛苦之後才設計出來協助大家的吧。
這裡是微軟Migrating系列裡對master pages的介紹:
Master pages are a new feature introduced in ASP.NET 2.0 to help you reduce development time for Web applications by creating a single place to maintain a consistent look and feel in a site. Master pages allow you to create a template that can be used to generate a common layout for many pages in the application. The primary goal of master pages is to avoid creating each page from scratch and having to repeat the layout code. Another benefit of using master pages is that if you want to change the layout of the pages in the application, you only have to update the master page, rather than each individual page.
Master Pages是ASP.NET 2.0裡介紹的一個新特色,通過建立一個來控制一致的樣式的頁面,可以協助你縮短開發web應用程式的時間。Master Pages允許你建立一個模板,這個模板用來產生一個使程式中的頁面(如果需要樣式一致的)具有相同的布局。Master pages的主要目的是為了避免我們從一個已經建好的頁面裡挖出那些控制頁面樣式的代碼,然後在重複它們的建立頁面的方法。使用Master pages的另一個好處是如果你想改變程式中頁面的樣式的時候,你只需要來改變這個master page就可以了,不用再一個一個的單獨修改了。
A master page looks like any ordinary ASP.NET Web page except for the extension (.master instead of .aspx) and some special controls and header fields. Master pages must contain one or more <asp:ContentPlaceHolder> controls. These controls represent areas of replaceable content. Basically, anything that is not in a ContentPlaceHolder will appear on any page that uses the master page.
一個master page頁面看起來像其他的普通的ASP.NET頁面,除了副檔名由.master 來替代了.aspx和一些特殊的控制項和標題欄位。Master pages必須包含一個和更多的<asp:ContentPlaceHolder>控制項。這些控制項表示了那些需要替代的內容。基本上那些不在ContentPlaceHolder的內容都將出現在使用master page的頁面裡了。
Other than these key changes, a master page can contain any HTML or control that can be found on a normal ASP.NET page
除了這些關鍵地方的改變外,master page頁面能包含一個普通的ASP.NET頁面上能夠存在的任意的HTML代碼或者是控制項。
看到這裡,master pages可能和frame很像了吧,其實他們是不一樣的,MS這樣說他們的區別:
Although master pages and frames serve a similar purpose, master pages offer much more functionality. Unlike frames, using master pages allow you to:
·  Bookmark a page and recall all the information on the specific page, not just the default frame page. A master page isn't really a frame. It's a single page that contains collated content from the master page and the content page that builds on the master. Therefore it looks and acts like a single Web page rather than a frame.
·   Work in controls and tags rather than HTML. Thanks to Visual Studio, you don't have to worry about opening and closing the frame tags or modifying countless html attributes to ensure that each frame displays in the fashion you want. You can simply create the place holder and modify its properties through Visual Studio.
·  Leverage Visual Studio's code creation to visually design the layout, manage the frames, and provide all of the plumbing to link the content pages into the master page. You can add new content without having to worry that the overall HTML layout of the page will be affected.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.