也想飞翔

笑看天边云卷云舒
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

获取和设置MasterPage 中控件的值

Posted on 2007-03-12 22:19  叶翔  阅读(1535)  评论(0编辑  收藏  举报
1、如果要获取母板页中的某一个控件的值,可以使用 Master.FindControl() 方法。
母板页部分代码...
<asp:Label ID="labNotice" runat="server" Text="Label001" ></asp:Label>
...



内容页中的部分code...
Label myLabel=new Label();
myLabel=(Label)Master.FindControl("labNotice");
string myText=myLabel.Text;

myText 的值就是"Label001"

2、可以通过创建@MasterType 指令 创建对此母板页的强类型饮用。

下面掩饰如何设置母板页中 Calendar控件的SelectedDate 属性的值。

首先在MasterPage.cs 中添加如下代码

MasterPage.cs...

/// <summary>
    /// mpCalendarDate 的摘要说明, 强命名MasterPage ,设置 MasterPage 公共属性值
    /// </summary>
    public DateTime mpCalendarDate
    {
        get
        {
            return Calendar1.SelectedDate;
        }
        set
        {
            Calendar1.SelectedDate = value;
        }
    }
...


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

...

注意,内容页中添加一行:<%@ MasterType VirtualPath="~/MasterPage.master" %>  这就是强引用。

内容页.cs
                 DateTime Date = DateTime.Now;

                /// <summary>
                /// mpCalendarDate 的摘要说明, 强命名MasterPage ,设置 MasterPage 公共属性值
                /// 内容页需要添加强命名MasterPage:<%@ MasterType VirtualPath="~/MasterPage.master" %>
                /// </summary>
                Master.mpCalendarDate = Date;

...

这样就可以使用代码动态来设置母板页中控件的值了。