首先,我們需要打開VS2005,創建一個WebSite(這裏要注意,是創建一個WebSite而不是創建一個Project) 然後,給剛建立的WebSite添加一個名字叫MasterPage.master的MasterPage,這個時候我們會看到該文件有如下內容:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>
下面用<Table>做下示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<table width="60%">
<tr>
<td colspan="3" style="height:80px">
<asp:ContentPlaceHolder ID="TopContent" runat="server">
</asp:ContentPlaceHolder>
</td>
</tr>
<tr>
<td style="width:30">
<asp:ContentPlaceHolder ID="LeftContent" runat="server">
</asp:ContentPlaceHolder>
</td>
<td>
<asp:ContentPlaceHolder ID="RightContent" runat="server">
</asp:ContentPlaceHolder>
</td>
</tr>
<tr>
<td colspan="3">
<asp:ContentPlaceHolder ID="FootContent" runat="server">
</asp:ContentPlaceHolder>
</td>
</tr>
</table>
</form>
</body>
</html>
有 了一個創建好的MasterPage後,我們就可以進行下面的工作了,現在我們創建一個aspx頁面,名字叫Default2.aspx,注意創建的時候 一定要勾上Select master page,創建的時候會有一個選擇模板的界面,由於只創建了一個模板,所以直接選擇MasterPage.master就行了,如果你創建了多個模板頁的 話,就需要在這裏進行選擇你當前頁面所需要加載的模板頁。
現在,我們有了一個基於模板的aspx頁面Default2.aspx,代碼如下:
<asp:Content ID="Content1" ContentPlaceHolderID="TopContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="LeftContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="RightContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="FootContent" Runat="Server">
</asp:Content>
現在我們來看下Default2.aspx的設計界面,看起來和剛纔我們創建的MasterPage.master很像,但是我們發現在Default2.aspx中,除了Content內部以外,其他地方都是不能編輯的。
設 計好它以後可以統一每個頁面的頁眉,頁腳.新建一個MasertPage後,再新建其他頁面的時候只要選擇好模板頁,然後在 contentplaceholder裏寫好你需要的東西.那麼運行後就不僅有你寫的東西有了,模板頁的東西也會自己出現,可以減少很多重複工作.
<%@ Master Language="C#" AutoEventWireup="true" Inherits="DuwControls.DuwDefaultMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<asp:contentplaceholder id="CHead" runat="server">
</asp:contentplaceholder>
</head>
<body>
<form id="form1" runat="server">
<asp:ContentPlaceHolder ID="ScriptMan" runat="server">
ScriptMain
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="CTop" runat="server">
CTop
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="CLeft" runat="server">
CLeft
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="CMain" runat="server">
CMain
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="CRight" runat="server">
CRight
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="CFoot" runat="server">
CFoot
</asp:ContentPlaceHolder>
</form>
</body>
</html>
//模版頁後臺,繼承自系統 System.Web.UI.MasterPage
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web;
namespace DuwControls
...{
public class DuwDefaultMasterPage : MasterPage
...{
private ContentPlaceHolder _CHead;
private ContentPlaceHolder _CTop;
private ContentPlaceHolder _CLeft;
private ContentPlaceHolder _CMain;
private ContentPlaceHolder _CRight;
private ContentPlaceHolder _CFoot;
private ContentPlaceHolder _ScriptMan;
public ContentPlaceHolder CHead ...{ get ...{ return _CHead; } }
public ContentPlaceHolder CTop ...{ get ...{ return _CTop; } }
public ContentPlaceHolder CLeft ...{ get ...{ return _CLeft; } }
public ContentPlaceHolder CMain ...{ get ...{ return _CMain; } }
public ContentPlaceHolder CRight ...{ get ...{ return _CRight; } }
public ContentPlaceHolder CFoot ...{ get ...{ return _CFoot; } }
public ContentPlaceHolder ScriptMan ...{ get ...{ return _ScriptMan; } }
protected override void OnInit(EventArgs e)
...{
foreach (Control ctrl in this.Controls)
...{
if (ctrl is System.Web.UI.HtmlControls.HtmlHead)//模版Head頭部部分
...{
foreach (Control ctl in ctrl.Controls)
...{
if (ctl is ContentPlaceHolder)
...{
_CHead = ctl as ContentPlaceHolder;
}
}
}
else if (ctrl is System.Web.UI.HtmlControls.HtmlForm)//模版Form主體部分
...{
foreach (Control ctl in ctrl.Controls)
...{
if (ctl is ContentPlaceHolder)
...{
switch (ctl.ID)
...{
case "CTop":
_CTop = ctl as ContentPlaceHolder;
break;
case "CLeft":
_CLeft = ctl as ContentPlaceHolder;
break;
case "CMain":
_CMain = ctl as ContentPlaceHolder;
break;
case "CRight":
_CRight = ctl as ContentPlaceHolder;
break;
case "CFoot":
_CFoot = ctl as ContentPlaceHolder;
break;
case "ScriptMan":
_ScriptMan = ctl as ContentPlaceHolder;
break;
default:
break;
}
}
}
}
}
base.OnInit(e);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using DuwCompontents;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
namespace DuwControls
...{
public class DuwPage : Page
...{
屬性屬性
事件事件
}
Inherits="DuwControls.DuwPage" SkinType="default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="CHead" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ScriptMan" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="CTop" Runat="Server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="CLeft" Runat="Server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="CMain" Runat="Server">
</asp:Content>
<asp:Content ID="Content6" ContentPlaceHolderID="CRight" Runat="Server">
</asp:Content>
<asp:Content ID="Content7" ContentPlaceHolderID="CFoot" Runat="Server">
</asp:Content>
版權所有:天天網絡工作室 晉ICP備06006449號
電話:0351-2149301 手機:13103416120