页面布局就使用MasterPage.master中的,而内容就使用Default2.aspx中Content控件下的
页面请求的次序
当用户请求一个用 Master Page 构建的页面时,各种事件发生的次序如下:
Master Page 子控件初始化;
内容页面子控件初始化;
Master Page 初始化;
内容页面初始化;
内容页面 Page_Load;
Master Page 的 Page_Load;
Master Page 子控件加载;
内容页面子控件加载;
注意点:
因为内容页面的 Page_Load 先于 Master Page 的 Page_Load,所以,如果要访问 Master Page 里的服务器控件,则必须在内容页面的 Page_LoadComplete 方法里书写代码
页面里用 <asp:Content /> 来添加内容到对应的空位:
<asp:Content ID="Content1" ContentPlaceHolderId="ContentPlaceHolder1" Runat="Server">
内容
</asp:Content/>
访问 MasterPage 中的属性和控件。
用 Master 属性来访问。
a. 假设 MasterPage 中有一个 Label1, 那么在内容页面可以这样:
protected void Page_LoadComplete(object sender, EventArgs e)
{
string text = (Master.FindControl("Label1") as Label).Text;
}
页面请求的次序
当用户请求一个用 Master Page 构建的页面时,各种事件发生的次序如下:
Master Page 子控件初始化;
内容页面子控件初始化;
Master Page 初始化;
内容页面初始化;
内容页面 Page_Load;
Master Page 的 Page_Load;
Master Page 子控件加载;
内容页面子控件加载;
注意点:
因为内容页面的 Page_Load 先于 Master Page 的 Page_Load,所以,如果要访问 Master Page 里的服务器控件,则必须在内容页面的 Page_LoadComplete 方法里书写代码
页面里用 <asp:Content /> 来添加内容到对应的空位:
<asp:Content ID="Content1" ContentPlaceHolderId="ContentPlaceHolder1" Runat="Server">
内容
</asp:Content/>
访问 MasterPage 中的属性和控件。
用 Master 属性来访问。
a. 假设 MasterPage 中有一个 Label1, 那么在内容页面可以这样:
protected void Page_LoadComplete(object sender, EventArgs e)
{
string text = (Master.FindControl("Label1") as Label).Text;
}