• 变通的模板实现思路 —— 基于 asp.net2.0 母版页


    传统的 asp,或者 php 都有很多分离程序和界面的模板引擎,有很多是基于正则查找,文本替换,然后输出的。在 asp.net2.0 中,因为母版页的出现,我摸索出一中新的方式:基于母版页实现界面和程序分离。

    实现思路
    在一个网站多个页面中,可能一些地方相同,那么我们可以使用同一个母版页。母版页因为对于美工来说,很吃力。我们需要做的就是让美工制作 html 页面,我们通过程序将 html 页面转换成 asp.net 的母版页。

    具体方法

    美工制作的 html 形式:

    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Default.aspx</title>
    <link rel="stylesheet" type="text/css" href="css/Global.css" />
    <link href="css/Page.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
    <div id="content" class="content">
      
    <div id="innerContent">
        
    <%contentplaceholder id="MainContent" %>
      
    </div>
    </div>
    </body>
    </html>
    关键的是这点代码:
    <%contentplaceholder id="MainContent" %>
    这就是我们模板的标签形式,为了方便美工编辑,两端使用 asp 程序的形式。通过替换 "<%" 为 " <asp: "、替换 "%>" 为 "runat="server" />"
    标签就变成这样了:
    <asp:contentplaceholder id="MainContent"  runat="server" />
    这正是 asp.net 中的控件声明的代码。同理,同样可使用此方式应用于用户控件。这样就可以让美工插入简单的标签即可,母版页就自动生成出来了。
    如果很多页面相同,则可以使用同一母版页,对应美工就只写一个 html 页面即可!
    通过页面的 Page_PreInit 事件,我们还可以动态加载母版页,以实现多风格站点的瞬间切换。

    使用案例:http://www.shanrui.net/
  • 相关阅读:
    vs2005发布生成自定义dll
    模拟msn消息提示(右下角)
    通过GridView导出Excel
    在ASP.NET 2.0中直接得到本页面生成的HTML代码
    asp.net实现SQL Server备份还原
    通用分页存储过程算法(.net类实现)
    超链接打开自定义的协议
    GridView技巧2
    sql语句获取本周、本月数据
    asp.net开发自定义控件
  • 原文地址:https://www.cnblogs.com/lspcieee/p/876112.html
Copyright © 2020-2023  润新知