由于先加载内容页,后加载母版页。所以不能用常规的方法在内容页中直接访问母版页的属性和方法。
但是asp.net2.0还提供了一个Page_LodeComplete()方法,我们可以在此函数内用Master.FindControl(控件ID)来访问母版页的属性和方法。如在母版页中有一个Label控件,ID为lblTime,记录当前的时间.同理,在内容页中也有一个同样的Label控件,也记录时间,其Id为currentTime;
<script runat="server">
public void Page_Load(object sender,EventArg e)
{
string strCurrentTime="";
strCurrentTime=System.DateTime.Now.ToShortTimeString();
}
</script>
在内容页的中
<script runat="server">
public void Page_Complete(object sender,EventArg e)
{
this.currentTime.Text=(Master.FindControl("lblTime") as Label).Text;
}
</script>
PS:此方法是获取母版页的属性。
那么 如何在内容页中设置母版页的属性呢?
首先必须弄清楚母版页和内容页的加载顺序。
因为先加载内容页,后加载母版页。所以可以直接在内容页的Page_Load()中设置母版页的相关属性,不过还得用到Master.FindControl()方法。