[.NET Webform]_MasterPage應用_化重複為統一化局部為彈性(一)基本介紹
MasterPage組件成員:
Master Page檔案(.master):定義網頁一致外觀的網頁檔案。
ContentPlaceHolder控制項: 定義各網頁可客製化的位置。
Content Page 檔案(.aspx):與Master Page結合的客製化網頁檔案。
Content控制項:對應主板頁面的ContentPlaceHolder控制項,用來放置客製化網頁內容。
MasterPage 主要是用於化零碎為統整,將相似重複的網頁結構、樣式統整在一塊,利於大型整站式網站開發。
於專案、網站中要新增時候選擇主版頁面(後墜.master)
.master預設的程式架構
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> <!DOCTYPE html> <html> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <asp:ContentPlaceHolder ID="head" runat="server"> </asp:ContentPlaceHolder> </head> <body> <form id="form1" runat="server"> <div> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> </form> </body> </html> |
在檔案內容中可以看到除了在Header跟Body中多了所謂ContentPlaceHolder的容器Tag
其次.master不屬於獨立存在的頁面(.aspx)因此也不能直接預覽,
若要引用我們剛剛才建立的MasterPage就要再新增一般的WebForm頁面(aspx)來進行引用。
最終生成的aspx檔案結構內容
這裡做一個測試
在剛剛生成的子頁.aspx中改寫
Default.aspx(已經和Master產生掛鉤的)
1 2 3 4 5 6 7 | <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <p>Sub Page Content</p> </asp:Content> |
MasterPage.master
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> <!DOCTYPE html> <html> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <asp:ContentPlaceHolder ID="head" runat="server"> </asp:ContentPlaceHolder> </head> <body> <form id="form1" runat="server"> <div> <p>On the top of contentpage</p> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> <p>below the contentpage</p> </div> </form> </body> </html> |
執行結果就可以明顯看到
contentplaceholder 這個容器的局部活動面板真名符其實啊!
更也凸顯出所謂的重複利用樣板的觀念
MasterPage中多個內容子頁機制
一個MasterPage下可有多個ContentPlaceHolder 依照其ID來進行辨別
這裡我在MasterPage檔案中去微調
多增加一個ContentPlaceHolder
MasterPage.master
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> <!DOCTYPE html> <html> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <asp:ContentPlaceHolder ID="head" runat="server"> </asp:ContentPlaceHolder> </head> <body> <form id="form1" runat="server"> <div> <p>On the top of contentpage</p> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> <p>Inside first content placeholder</p> </asp:ContentPlaceHolder> <p>below the contentpage</p> </div> <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server"> <p>Inside second content placeholder</p> </asp:ContentPlaceHolder> <p>below the second content placeholder</p> </form> </body> </html> |
於aspx中改為
嵌入一張狗狗圖跟文字的內容子頁
aspx
1 2 3 4 5 6 7 8 9 10 11 12 13 | <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"> <p>Sub Page Content2</p> <img src="img/dog.jpg" /> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <p>Sub Page Content</p> </asp:Content> |
可以注意到順序是依照MasterPage中放置的ContentPlaceHolder順序
ID依序是先ContentPlaceHolder1再ContentPlaceHolder2
所以即便aspx檔案中誰前誰後都無相關
最終render出來的完整html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <!DOCTYPE html> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title> </title> </head> <body> <form method="post" action="./Default.aspx" id="form1"> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="eeq+4wuRGgZLykMeLUVFMS483HKB+GmP0KWphvQMmOKTR7IOyOxphPL9t4JwdBwmQnCZD13vKxPNsQCPwDppeuTpi91rIXn5qDpnT/q14BA=" /> </div> <div> <p>On the top of contentpage</p> <p>Sub Page Content</p> <p>below the contentpage</p> </div> <p>Sub Page Content2</p> <img src="img/dog.jpg" /> <p>below the second content placeholder</p> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="CA0B0334" /> </div></form> <!-- Visual Studio Browser Link --> <script type="text/javascript" src="http://localhost:7444/0e9c49098bfe421e82c53c892e7db716/browserLink" async="async" id="__browserLink_initializationData" data-requestId="6975da906b684aff9481cdf5b178488e" data-appName="Chrome"></script> <!-- End Browser Link --> </body> </html> |
MasterPage中 css/js如何串聯?
由於MasterPage下的各個ContentPage一般都需要一致性外觀樣式、布局...etc
因此通常都是直接統一放置在.master檔案中
這裡就先拿CSS做個實驗
新增一個是CSS是for master 對<p>內容改紅色
拖曳至於.master <head>區塊中
MasterPage.master
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> <!DOCTYPE html> <html> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="css/WebStyleForMaster.css" rel="stylesheet" type="text/css" /> <asp:ContentPlaceHolder ID="head" runat="server"> </asp:ContentPlaceHolder> </head> <body> <form id="form1" runat="server"> <div> <p>On the top of contentpage</p> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> <p>Inside first content placeholder</p> </asp:ContentPlaceHolder> <p>below the contentpage</p> </div> <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server"> <p>Inside second content placeholder</p> </asp:ContentPlaceHolder> <p>below the second content placeholder</p> </form> </body> </html> |
最終render出來的完整html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <!DOCTYPE html> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title> </title><link href="css/WebStyleForMaster.css" rel="stylesheet" type="text/css" /> </head> <body> <form method="post" action="./" id="form1"> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="A1KYIizo8POXsJiL8Fkb/aDQPvLAQtEFRSl+GWVfUuuGO6CMQPTdABPZFmm/zw1vCnPzsx/hmMqCoQHVLclvMvV9DwLdT9VpXyUU4spPl60=" /> </div> <div> <p>On the top of contentpage</p> <p>Sub Page Content</p> <p>below the contentpage</p> </div> <p>Sub Page Content2</p> <img src="img/dog.jpg" /> <p>below the second content placeholder</p> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="CA0B0334" /> </div></form> <!-- Visual Studio Browser Link --> <script type="text/javascript" src="http://localhost:7444/0e9c49098bfe421e82c53c892e7db716/browserLink" async="async" id="__browserLink_initializationData" data-requestId="fd07ac9432614977884c80c40d0b2c9e" data-appName="Chrome"></script> <!-- End Browser Link --> </body> </html> |
一個CSS是for aspx 子頁的 對<p>內容改藍色
如果同時設定會發生捨麼事情?(優先級)
在子頁(.aspx)中拖曳至ContentPlaceHolderID="head" 區塊中
最終render出來的完整html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <!DOCTYPE html> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title> </title><link href="css/WebStyleForMaster.css" rel="stylesheet" type="text/css" /> <link href="css/WebStyleForASPX.css" rel="stylesheet" type="text/css" /> </head> <body> <form method="post" action="./Default.aspx" id="form1"> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="pGT53UpBiCuMkb5IfUDd8wsAYGDT0Jmii0vZMQfo8mWQ4M8HFvXOynozPfE82q5WDQLLfZu5rkrojrnMx/lU8ItAIwWjQBK/8e+/Iw7ytGw=" /> </div> <div> <p>On the top of contentpage</p> <p>Sub Page Content</p> <p>below the contentpage</p> </div> <p>Sub Page Content2</p> <img src="img/dog.jpg" /> <p>below the second content placeholder</p> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="CA0B0334" /> </div></form> <!-- Visual Studio Browser Link --> <script type="text/javascript" src="http://localhost:7444/0e9c49098bfe421e82c53c892e7db716/browserLink" async="async" id="__browserLink_initializationData" data-requestId="6b71cdb576ff4fc59d28b5eb8a294dbc" data-appName="Chrome"></script> <!-- End Browser Link --> </body> </html> |
可以看到ContentPage(aspx)後期額外自定義的CSS由於在Master之後
(因為MasterPage.master中css定義位置在比ContentPlaceHolder ID="head"還要上面的位置)
因此在之後定義的於子頁ContentPlaceHolderID="head"區塊的CSS
會蓋掉原本master定義的css
如果將Master定義CSS的位置移動到ContentPlaceHolderID="head"之下
就會顯示紅色了
無論在子頁或母頁都可依照實際專案需求來做CSS位置調整設置
當CSS相對路徑對於MasterPage和CotnentPage不同路徑時狀況
這裡我們延續剛的樣式規則(只保留轉紅色的)
於另外目錄(名test)新增額外的子頁一樣掛勾我們的Master
會發現<p>仍是紅色
但子頁寫的test是紅色
會一樣是紅色在於css會依據子頁去設定
對deafult2.aspx要套用css就必須返回上一層後在到css目錄下設置相應css
對default.aspx而言則是
直接到css目錄下設置相應css
留言
張貼留言