您现在的位置:中国下载站学院中心网络编程ASP.net教程Asp.Net开发技巧 → 文章列表

ASP.NET 2.0 中的母版页详解

作者:佚名  来源:不详  发布时间:2006-12-29 0:18:22   

减小字体 增大字体

 
 
为了减少在网页设计时出现的变一页则动全站的问题,vs2003升级到vs2005后增加了母版的概念。

  你可以把它想像成为“网页模版”,与之不同的是,再也不必每个页面都去更新了,修改一次,所有的网页都会改变,做到了一劳永逸。

  下面先进行一个简单的母版使用演示:

  1、首先打开visual studio 2005,新建一个asp.net网站,文件系统,C#。

  2、在解决方案资源管理器中,右键新建一个新项:

itbulo.com/UploadFiles_1485/200606/20060614100621367.jpg" onload="return imgzoom(this,550);" onclick="javascript:window.open(this.src);" style="cursor: pointer;"/>

  3、选择母版页:

itbulo.com/UploadFiles_1485/200606/20060614100622252.jpg" width=679 onload="return imgzoom(this,550);" onclick="javascript:window.open(this.src);" style="cursor: pointer;"/>

  4、打开MasterPage.master,里面有一个contentplaceholder控件,注意不要控件里面写什么东西。

  我们转到设计视图,在这个控件外面加上header和footer 两句文本。


itbulo.com/UploadFiles_1485/200606/20060614100623964.jpg" onload="return imgzoom(this,550);" onclick="javascript:window.open(this.src);" style="cursor: pointer;"/>

  5、保存后我们就可以用它来做其它页面了。有两种方法,1是在母版页任意位置右键,点击添加内容页;2 是在解决方案资源管理器上新建新项,在生成aspx页面时勾选“选择母版页”

itbulo.com/UploadFiles_1485/200606/20060614100623359.jpg" onload="return imgzoom(this,550);" onclick="javascript:window.open(this.src);" style="cursor: pointer;"/>

  6、选择相应的母版页

itbulo.com/UploadFiles_1485/200606/20060614100625714.jpg" onload="return imgzoom(this,550);" onclick="javascript:window.open(this.src);" style="cursor: pointer;"/>

  7、在新生成的页面源代码只有这几句:

 

  1. <%@PageLanguage="C#"MasterPageFile="~/MasterPage.master"AutoEventWireup="true"
      CodeFile="Default2.aspx.cs"Inherits="Default2"Title="UntitledPage"%>
  2. <asp:ContentID="Content1"ContentPlaceHolderID="ContentPlaceHolder1"Runat="Server">
  3. </asp:Content>

  我们可以看到一个content控件,这个东西对应母版页的ContentPlaceHolder1控件,转换到视图页面:

itbulo.com/UploadFiles_1485/200606/20060614100626397.jpg" onload="return imgzoom(this,550);" onclick="javascript:window.open(this.src);" style="cursor: pointer;"/>


  8、其中页头和页脚的文字都是灰色的,我们只能在 content中进行编辑。

  保存后访问default2.aspx这个页面,F5.,我们看到页面:

itbulo.com/UploadFiles_1485/200606/20060614100627258.jpg" onload="return imgzoom(this,550);" onclick="javascript:window.open(this.src);" style="cursor: pointer;"/>
 9、和想像的一样吧,我们再来看看源代码:

 

  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <htmlxmlns="http://www.w3.org/1999/xhtml">
  3. <head><title>
  4. UntitledPage
  5. </title></head>
  6. <body>
  7. <formname="aspnetForm"method="post"action="Default2.aspx"id="aspnetForm">
  8. <div>
  9. <inputtype="hidden"name="__VIEWSTATE"id="__VIEWSTATE"
       value="/wEPDwULLTEwMDUyNjYzMjhkZASHJAhe9XmxUHPbOeONMX2y6XYi"/>
  10. </div>
  11. <div>
  12. thisisthepage'sheader<br/>
  13. thispage'scontent:hello,world!<br/>
  14. thisisthepage'sfooter </div>
  15. </form>
  16. </body>
  17. </html>

  母版内容是放在一个div中的,而content页面并没有放在单独的div,就是说在母版不会给子页添加任何的多余代码。这就给我们编程和网页布局带来了很大的灵活性,我们可以充分利用CSS+DIV的形式定位,亦可以用table方式进行定位。修改时也不必每个页面都去修改。

  10、对于不是一块固定内容的母版,我们可以用多个ContentPlaceHolder1来进行布局,下面的例子是用table来定位的:

itbulo.com/UploadFiles_1485/200606/20060614100628391.jpg" onload="return imgzoom(this,550);" onclick="javascript:window.open(this.src);" style="cursor: pointer;"/>

  11、在相应的子页面里会有两个content:

itbulo.com/UploadFiles_1485/200606/20060614100629894.jpg" onload="return imgzoom(this,550);" onclick="javascript:window.open(this.src);" style="cursor: pointer;"/>

  生成的代码:

 

  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <htmlxmlns="http://www.w3.org/1999/xhtml">
  3. <head><title>
  4. UntitledPage
  5. </title></head>
  6. <body>
  7. <formname="aspnetForm"method="post"action="Default3.aspx"id="aspnetForm">
  8. <div>
  9. <inputtype="hidden"name="__VIEWSTATE"id="__VIEWSTATE"
      value="/wEPDwUKMTY1NDU2MTA1MmRkPjWLPyqA5JXcW5ivHc0NiYajQTU="/>
  10. </div>
  11. <div>
  12. thisisthepage'sheader<br/>
  13. <table>
  14. <tr>
  15. <td>
  16. ohmyContent1
  17. </td>
  18. <td>
  19. himyContent2
  20. </td>
  21. </tr>
  22. </table>
  23. thisisthepage'sfooter 
  24. </div>
  25. </form>
  26. </body>
  27. </html>

  12、要灵活应用,CSS虽然也可以用DIV来解决这个问题,但是一些非标准控件的样子是很难用CSS来控制的,如果你做了另外一套母版MasterPage2.master,你可在页面中动态设置:

 
protected void Page_PreInit(object sender, EventArgs e)
{
 MasterPageFile = "~/MasterPage2.master";
}

  先到这里,据说还可以嵌套应用,不过目前是够用了,配合一下theme的使用,下次再说。

在百度中搜索更多ASP.NET 2.0 中的母版页详解相关网页 转贴于:中国下载站

  • 上一篇文章:ASP.NET 2.0的页面缓存功能介绍
  • 下一篇文章:ASP.NET 2.0服务器控件开发之简单属性
  • 阅读统计:[]
  • 中国下载站】【设为主页】【收藏本页】【打印本文】【回到顶部】【关闭此页

    相关文章
    文章评论(评论内容只代表网友观点,与本站立场无关!)

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 0分

    内 容:

             (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码


    设为首页 - 关于我们 - 广告服务 - 网站地图 - 加入收藏 - 网站声明 - 网站帮助 - 友情链接

    • Copyright (C) 2006-2008 www.cndownz.com All Rights Reserved.
      中国下载站 版权所有. 粤ICP备05141802号. 对本站有任何建议、意见或投诉,请来信:cndownzcom@yahoo.com.cn.
      喜欢中国下载站(cndownz.com),请把中国下载站(cndownz.com)告诉你QQ上的5位好友,多谢支持!