信阳农夫的专栏

志向立高远,学问做精细!努力做一名合格的信阳农专人!

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

代码片段:

 MainMaster.master 主母版

 

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MainMaster.master.cs" Inherits="MasterPage" %>

 <!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>xynf07.cn</title>

<style type="text/css">

body

{

    margin:0 auto;

    padding:0 auto;

}

</style>

</head>

<body>

   <div style="width:889px;margin:0 auto;padding:0 auto;text-align:center;"><form id="form1" runat="server">

        <table style="width:100%;height:90px;text-align:center;background-image:url(images/banner.gif);border:0px;" cellpadding="0" cellspacing="0" id="_01">

         <asp:ContentPlaceHolder ID="DisplayDate" runat="server">

            <tr><td style="height:30px;text-align:right;">

            <asp:Label ID="labMaster" runat="server"></asp:Label>

            </td></tr>

         </asp:ContentPlaceHolder>

        <tr><td style="height:60px;vertical-align:top;"><h2>Welcome to www.xynf07.cn !</h2></td></tr>

        </table>

 

        <table style="width:100%;height:550px;border:0px;" cellpadding="0" cellspacing="0" id="Table1">

        <tr><td style="vertical-align:top;">

            <asp:ContentPlaceHolder ID="MainContent" runat="server">

            <h1 style="width:186px">主¡Â</h1><p> </p>

            </asp:ContentPlaceHolder>

        </td></tr>

        </table>

         <table style="width:100%;height:89px;text-align:center;" border="0" cellpadding="0" cellspacing="0" id="__01">

        <tr><td style="background-color:#003466;" >版ã?权¨¡§所¨´有®D ©2011-<a href="http://www.xynf07.cn">www.xynf07.cn</a></td></tr>

        </table>

    </form>

</div>

</body>

</html>

 SubMaster.master 子母版

 

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="SubMaster.master.cs" MasterPageFile="~/MainMaster.master" Inherits="SubMaster" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <table style="width:889px;height:550px;text-align:center;" border="0" cellpadding="0" cellspacing="0" id="__01">

        <tr>

        <td style="width:200px;background-color:#E8E8E8;border:1px solid #A9CBEE;"><h1>左Á¨®侧¨¤边À?栏¤?</h1></td>

        <td style="width:689px;background-color:#F5F5F5;border:1px solid #A9CBEE;">

            <asp:ContentPlaceHolder ID="SubContent" runat="server">

            <h1 style="width:186px">内¨²容¨Y区?</h1>

            <p> </p>

            </asp:ContentPlaceHolder>

        </td>

        </tr>

    </table>

</asp:Content>

 Default.aspx 内容页

 

<%@ Page Title="www.xynf07.cn" Language="C#" MasterPageFile="~/SubMaster.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="SubContent" Runat="Server">

    <table style="width:300px;height:550px">

    <tr><td style="vertical-align:top;">

         <asp:Label ID="labContent" runat="server"></asp:Label>

        </td></tr>

    </table>

</asp:Content>

 Default.aspx.cs

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_LoadComplete(object sender, EventArgs e)

    {

        Label MLable1 = (Label)this.Master.Master.FindControl("DisplayDate").FindControl("labMaster");

        labContent.Text = MLable1.Text;

    }

}

知识点:

1)母版页占位符内一般是不放置东西的,只是匹配内容页的;

2)Label MLable1 = (Label)this.Master.Master.FindControl("DisplayDate").FindControl("labMaster");

a)这里内容页继承了两次母版(主母版和子母版);

母版页中:<asp:contentplaceholder id="DisplayDate" runat="server"></asp:ContentPlaceHolder>只是个标记,运行后.net系统将这个标记替换成内容页<asp:Content ID="Content1" ContentPlaceHolderID="DisplayDate" Runat="Server"></asp:Content>里的内容。

b)this.Page.Master.FindControl(masterPageContentPlaceHolderID).FindControl("ControlName");
调用母版页中的控件(包含在占位符里);我这里因为是主母版,所以显示Master两次。

 

关于:ContentPlaceHolder 控件

 

ContentPlaceHolder 控件为母版页中的内容定义相对区域,并呈现在内容页中找到的相关 Content 控件中的所有文本、标记和服务器控件。

 

Content 控件与使用其 ContentPlaceHolderID 属性的 ContentPlaceHolder 相关联。请将 ContentPlaceHolderID 属性设置为母版页中相关的 ContentPlaceHolder 控件的 ID 属性的值。在一个母版页中可以声明多个 ContentPlaceHolder 。

 

在内容页中,只有一个 Content 控件可以为母版页中的 ContentPlaceHolder 提供内容。但是,在每个使用母版页的内容页中,可以有单独的 Content 控件与该 ContentPlaceHolder 相关联。例如,可以为母版页中的网页标题定义 ContentPlaceHolder 。对于使用该母版页的每个内容页,均可添加一个 Content 控件来为网页标题提供文本和标记。

相关链接:http://msdn.microsoft.com/zh-cn/library/cc295268(v=Expression.40).aspx
posted on 2011-03-20 21:01  信阳农夫  阅读(409)  评论(0)    收藏  举报