zoukankan      html  css  js  c++  java
  • 在ASP.NET2.0中实现主页嵌套

    现在的很多商业公司都设有不同的部门,而这些部门在公司的网站上都有自己的子网站。一般情况下,每一个部门都会根据自己的需要来维护各自的网站。这样做虽然会使公司的网站显得丰富多彩,但这却会对用户的访问带来不便,也就是说,由于各个部门的子网站没有保持一致性而使用户在浏览网站时造成了困难。幸运的是,ASP.NET2.0为我们提供了一种解决方案,这就是主页嵌套。

     建立嵌套主页

    首先需要建立一个标准的主页,在这个主页上需要加上一些共用的东西,如公司的Logo、公司名称、页脚以及菜单等,而每个部门的子网站必须使用这个标准的主页。每一个部门可以根据自己的业务需要建立各自的主页,然后将这些部门的主页嵌入刚才建立的标准中。这样做无论对最终的用户还是对每个部门都是有好处的,对于最终用户,无论他访问哪个部门的网站,都会看到同样的Logo、菜单以及页脚。而对于部门来说,他们可以自己建立一个内嵌的主页,这个主页的内容可以根据自己部门的业务需要而定。这就象是一个OCX控制嵌在网页上一样。

    为了理解如何建立嵌套主页,下面让我们看一个例子。首先给出一个标准主页的例子。

    以下是引用片段:
    <%@ Master Language="C#" AutoEventWireup="true"
    CodeFile="WebsiteMasterPage.master.cs" Inherits="WebsiteMasterPage" %>
    <html>
     <head runat="server" id="head">
        <title>标准主页例子</title>
     </head>
     <body>
       <form id="form1" runat="server">
         <table width="100%">
           <tr>
             <td bgcolor="#006633" colspan="2">
               <table border="0" cellpadding="0" cellspacing="0" width="100%">
           <tr>
                   <td align="left">
                     <a href="/MasterPage/Default.aspx">
                       <img alt="Home Page" border="0" src="/MasterPage/images/logo.gif" />
                     </a>
                   </td>
                   <td align="right">
                     <img src="/MasterPage/images/header_image.gif"/>
                   </td>
                 </tr>
               </table>
             </td>
           </tr>
           <tr>
             <td width="25%">
               <font color="#3300FF">部门1 <br /> 部门2 <br /></font>
             </td>
             <td width="75%">
                 <asp:ContentPlaceHolder ID="Main" runat="server">
                 </asp:ContentPlaceHolder>
             </td>
           </tr>
           <tr>
             <td colspan="2"> </td>
           </tr>
           <tr>
             <td bgcolor="#0000FF" colspan="2">
                   <font color="#FFFF00">注脚</font>
             </td>
           </tr>
         </table>
       </form>
     </body>
    </html>

       上面的标准主页定义了公司的Logo、注脚和菜单的位置。还定义了部门的主页要嵌入的位置(这个要使用ContentPlaceHolder控件)。部门主页的代码中和上面的代码有些不同,在部门主页的代码中需要引用上述的标准主页。这个可以通过在部门主页代码中加入MasterPageFile属性实现。下面是一个部门的主页代码:

    以下是引用片段:
    <%@ Master MasterPageFile="~/Templates/WebsiteMasterPage.master" Language="C#"   AutoEventWireup="true" CodeFile="NestedMasterPage.master.cs"   Inherits="NestedMasterPage" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="Main" runat="server">
    <table width="100%">
    <tr>
        <td style="background-color:BLUE; font-weight: bold; coloar: white">
                      <font color="#FFFF00" >部门主页 </font>
        </td>
      </tr>
      <tr>
         <td>
         <asp:ContentPlaceHolder ID="NestedMain" runat="server" />
         </td>
       </tr>
     </table>
    </asp:Content>


    从上面的代码可以看出其中引用了标准主页WebsiteMasterPage.master。而且还定义了一个服务端控件来引用在标准主页中定义的ContentPlaceHolder的ID(ID为Main)。由于部门主页被嵌套在标准主页中,因此,必须使用Content服务端控件。还有就是必须加入ContentPlaceHolder控件,这个控件指示了部门主页显示的位置。

        现在这个部门主页已经被嵌入到标准主页中了。部门的主页可以自动继承标准主页的Logo、注脚和菜单。如果要更换这些公共的元素,只需更新这个标准主页即可。而且各部门也可根据自己的需要来更新内嵌在标准主页的部门主页。程序运行界面如图1所示。

    在ASP.NET2.0中实现主页嵌套

    图1

    在Visual Studio2005中使用嵌套主页

      我们从上面的部门主页代码中可以看到,MasterPageFile属性引用了标准主页。但这个属性在Visual Studio2005中并不支持可视化编辑。因此,要想在VS2005的设计视图中编辑主页,必须将MasterPageFile设为空串。如下面的代码如示:

       

    以下是引用片段:
    <%@ Page Language="C#" MasterPageFile="" Title="部门主页" %>

      当我们将MasterPageFile设为空串后,在每次更新标准主页后在发布时都得手工来修改这个属性。如果不想这么麻烦的话,可以通过一些手段来欺骗一个Visual Studio .NET的设计视图。首先建立一个从System.Web.UI.Page继承的类(将这个类命名为BasePage)。在这个类中定义一个RuntimeMasterPageFile属性(这个属性可以使用任何名子)。实现代码如下:

    以下是引用片段:
    public class BasePage : System.Web.UI.Page {
            private string _RuntimeMasterPageFile;
     
            public string RuntimeMasterPageFile {
                get {
                    return _RuntimeMasterPageFile;
                }
                set {
                    _RuntimeMasterPageFile = value;
                }
            }
     
            protected override void OnPreInit(EventArgs e) {
                if (_RuntimeMasterPageFile != null) {
                    this.MasterPageFile = _RuntimeMasterPageFile;
                }
                base.OnPreInit(e);
            }
        }
     

        BasePage还重载了OnPreInit方法,以便在Aspx页装载时可以动态地设置MasterPageFile属性。实现了BasePage类后,以后需要内嵌的aspx中的类就可以直接从BasePage继承了。

    以下是引用片段:
        public partial class MyNestedMaster : BasePage {
     
            // 具体的实现代码
        }
     

        下面我们来修改.aspx文件。首先将MasterPageFile属性设为空串,并且将RuntimeMasterPageFile属性直接加到aspx文件中,并将它的值设为内嵌主页的路径。然后设置CodeFileBaseClass属性为"BasePage",实现代码如下:

    以下是引用片段:
    <%@ Page Language="C#"   MasterPageFile=""
    RuntimeMasterPageFile="~/Templates/NestedMasterPage.master"
    CodeFileBaseClass="BasePage"
    Inherits="MyNestedMasterAndBasePage" AutoEventWireup="true"
    CodeFile="MyNestedMasterAndBasePage.aspx.cs Title="Page1" %>
     
    <asp:Content ID="ContentNested" runat="server" ContentPlaceHolderID="NestedMain">
    <p> &nbsp;</p> <p> &nbsp;</p>
       财务部主页
    <p> &nbsp;</p> <p> &nbsp;</p>
    </asp:Content>

      在运行时,BasePage类将被实例化,而MasterPageFile属性将被动态地设置为RuntimeMasterPageFile属性的值。

  • 相关阅读:
    linux内核系统调用和标准C库函数的关系分析
    Linux下内存映射文件的用法简介
    Hi35xx 通用GPIO 使用篇(板子3G电源控制脚说明)
    关于Linux用户名
    关于ARGB_8888、ALPHA_8、ARGB_4444、RGB_565的理解
    PixelFormat 图像颜色的数据格式
    Ubuntu 搭建svn服务器 ,以及常见错误解决方案
    安装VisualSVN Server 报错The specified TCP port is occupied
    SVN服务器搭建和使用(三)
    SVN服务器搭建和使用(二)
  • 原文地址:https://www.cnblogs.com/nianshi/p/794416.html
Copyright © 2011-2022 走看看