牛腩新聞發布系統(三)--母版
來源:程序員人生 發布時間:2015-01-29 08:55:03 閱讀次數:2417次
在做網頁的進程中,總會有很多地方變化不大,但是重復出現。
所以引入母版這個概念就是好,重復的地方寫1個母版,然后其他頁面只要援用就可以實現相同的功能,并且頁面的代碼也會減少很多。
比如我寫1個小demo
這是1個簡單的閱讀古詩的網頁

其中的頭部和尾部我都是不想換的,比如head和bottom兩個地方是網頁復用最多的地方。假定我有很多的網頁要做而這些地方又都1樣,那我不能重復的建這些吧。這里就用到了面向對象思想――抽象――母版頁。把head和bottom兩個部份放到母版頁中,只要其他頁面援用母版頁那末相同的功能就能夠不用再重復寫了。
修改前代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!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>靜夜思</title>
</head>
<body>
<center>
<form id="form1" runat="server">
<p align="center">
靜夜思<br />
作者:李白<br />
<b>床前明月光,疑是地上霜<br />
舉頭望明月,低頭思故鄉 </b>
</form>
<div>
<img src="../images/20110111⑴45047.jpg" width ="760" height="" alt="img" /></div>
<div id="footer">版權歸屬:copyright ©<a href ="http://www.csdn.net/mayfla" target="_blank">徐玲博</a>
</div>
</center>
</body>
</html>
修改后代碼:
母版頁:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.Site1" %>
<!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></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<center>
<div>
<img src="../images/4d0e5ddd695046884732f.jpg" width="760" height="160" alt="img" /></div>
<div>
<p>
<font color="green">古詩欣賞</font></p>
</div>
<div id="main">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
<div id="footer">
版權歸屬:copyright ©<a href="http://www.csdn.net/mayfla" target="_blank">徐玲博</a>
</div>
</center>
</form>
</body>
</html>
效果:

有了這個母板頁,靈活網頁也就開始了第1步。
內容頁的代碼和效果我就省略了。差不多。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈