浅入浅出讲解asp.net 制作模板页 收藏 很多时候我们制作一个网站的时候,web前端设计好了,总会或多或少的有那么些页面需要使用相似的布局或者设置,当然每一个页面都自己去进行相同的设计和操作是可以的,可是这样去降低了编程开发的效率,那么我们今天就用如何制作模板页来取代重复设计代码的繁琐,下面操作在Vs2010 IDE下进行: 1,首先选好重复使用的有相关布局和设置的页面, 举一个简单的例子,如下图<!--[endif]--> 模板的内容基本包括Logo,和版权信息 如下列代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="007.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./1999/xhtml" > <head runat="server"> <title></title> <link href="css/test.css" rel="stylesheet" type="text/css" /> </head> <body> <form id="form1" runat="server"> <div id="form"> <div id="logo" runat="server" class="logo"> Logo 加入模板 重复利用 </div> <div id="content" runat="server" class="content">content 可编辑区域</div> <div id="copyright" runat="server" class="copyright">版权信息 加入模板 重复利用</div> </div> </form> </body> </html> 下面开始添加母板,在网站目录下选择添加新项,选择母板页<!--[endif]--> 然后在将适当的模块加入*.master 文件中 代码如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./1999/xhtml" > <head runat="server"> <title></title> <link href="css/test.css" rel="stylesheet" type="text/css" /> <asp:ContentPlaceHolder ID="head" runat="server"> <!-- 需要编辑的地方要放上 <asp:ContentPlaceHolder ID="header" runat="server"> </asp:ContentPlaceHolder>--> </asp:ContentPlaceHolder> </head> <body> <form id="form1" runat="server" > <div> <div id="logo" runat="server" class="logo"> Logo 加入模板 重复利用 </div> <div id="content" runat="server" class="content">content 可编辑区域 <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> <!-- 需要编辑的地方要放上 <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder>--> </asp:ContentPlaceHolder></div> <div id="copyright" runat="server" class="copyright">版权信息 加入模板 重复利用</div>
</div> </form> </body> </html> 然后保存vs2010 的使用者在site1.master(即你刚刚创建的模板母版页右击选择添加内容页),其他的vs版本选择添加新项,web窗体 勾选下面的使用母板,然后得到一个新的.aspx的文件,将其设为启动页,得到的代码类似下面 <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> Hello********* </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> Google********* </asp:Content> 上面的******区域就是可以编辑的区域,其他的就是你母板的样式了,然后调试看一下就可以了,根据个人要求进行适当修改。 p.s. 注意在母版页设置的css文件需要适当修改。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/BossDarcy/archive/2010/11/28/6040591.aspx |
|
来自: orion360doc > 《母版页》