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

内容页访问母版页的属性及控件

Posted on 2011-02-18 10:16  itcfj  阅读(255)  评论(0编辑  收藏  举报

获取母版页的相关内容有两种方法

1 通过findcontrol找控件ID
 需要在此事件中~因为Page_load中时是先内容页加载然后才是母版页加载
    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        Label2.Text = "现在时间是" + (Master.FindControl("Label1") as Label).Text;
        if (Request.QueryString["id"] == "dy")
        {
            (Master.FindControl("Image1") as Image).ImageUrl = "~/Images/ml0069.jpg";
        }
    }

2 通过强引用

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/MasterPage.master" %>

然后可以在母版页中定义公共属性或方法
public string GetUserName()
    {
        return Page.User.Identity.Name;
    }
在内容页中调用
 Label1.Text = "欢迎光临" + Master.GetUserName();

一、使用FindControl方法获取母版页控件的引用
利用内容页page对象的Master公共属性,我们可以实现对关联母版页的引用。进而使用母版页的FindControl方法来实现对母版页控件的访问。
母版页MasterPage.master:

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

< !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>母版页< /title>
< /head>
< body>
    < form id="form1" runat="server">
    < asp:Label runat="server" ID="masterlabel">母版页的内容< /asp:Label>
    < div>
        < asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        < /asp:contentplaceholder>
    < /div>
    < /form>
< /body>
< /html>

内容页Content1.aspx:

< %@ Page Language="C#" MasterPageFile="~/MasterPage1.master" AutoEventWireup="true" CodeFile="content1.aspx.cs" Inherits="content1" Title="Untitled Page" %>
< script runat="server">
    void Page_LoadComplete(Object sender, EventArgs e)
    {
        contentlabel.Text = (Master.FindControl("masterlabel") as Label).Text;
    }
< /script>
< asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
< asp:Label ID="contentlabel" runat="server">这里将显示母版页masterlabel控件的内容。< /asp:Label>
< /asp:Content>
其中,“Page_LoadComplete”是内容页面加载完成时触发的一个事件。
运行结果:

二、使用MasterType指令获取母版页控件的引用
相对于上面的FindControl方法而言,MasterType显得很直接。通过使用MasterType,可以创建与母版页的强类型引用。

将FindControl方法例子中的MasterPage.master更改如下:

< %@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage1.master.cs" Inherits="MasterPage1" %>
< script runat="server">
    public Label MasterPageLabel//注意:将母版页label控件强类型化,方便内容页访问。对母版页属性的访问也使用该方法。
    {
        get#p#分页标题#e#
        {
            return masterlabel;
        }
        set
        {
            masterlabel = value;
        }
    }
< /script>
< !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>母版页< /title>
< /head>
< body>
    < form id="form1" runat="server">
    < asp:Label runat="server" ID="masterlabel">母版页的内容< /asp:Label>
    < div>
        < asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        < /asp:contentplaceholder>
    < /div>
    < /form>
< /body>
< /html>

将FindControl方法例子中的Content1.aspx更改如下:

< %@ Page Language="C#" MasterPageFile="~/MasterPage1.master" AutoEventWireup="true" CodeFile="content1.aspx.cs" Inherits="content1" Title="Untitled Page" %>
< %@ MasterType VirtualPath="~/MasterPage1.master" %>
< script runat="server">
    new void Page_Load(Object sender, EventArgs e)
    {
        contentlabel.Text = Master.MasterPageLabel.Text;
    }
< /script>
< asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
< asp:Label ID="contentlabel" runat="server">这里将显示母版页masterlabel控件的内容。< /asp:Label>
< /asp:Content>