ASP.NET 2.0比以前版本的一个较大的改进,就是提供了母版页 。那么它与内容页之间是怎么通讯的呢,可以参照如下事例,就能够实现其功能(代码很简单,就不加注释了 源代码下载):
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
// Master页面
public partial class MasterPage : System.Web.UI.MasterPage
... {
protected void Page_Load(object sender, EventArgs e)
...{
}
protected void btnMaster_Click(object sender, EventArgs e)
...{
Label lbl = this.ContentPlaceHolder1.FindControl("lblContent") as Label;
Response.Write("<script language='javascript'>alert('" + lbl.Text + "');</script>");
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
// Master页面
public partial class MasterPage : System.Web.UI.MasterPage
... {
protected void Page_Load(object sender, EventArgs e)
...{
}
protected void btnMaster_Click(object sender, EventArgs e)
...{
Label lbl = this.ContentPlaceHolder1.FindControl("lblContent") as Label;
Response.Write("<script language='javascript'>alert('" + lbl.Text + "');</script>");
}
}
内容页面如下:
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
// 内容页面
public partial class Default2 : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
}
protected void btnContent_Click(object sender, EventArgs e)
...{
Label lbl = this.Master.FindControl("lblMaster") as Label;
Response.Write("<script language='javascript'>alert('" + lbl.Text + "');</script>");
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
// 内容页面
public partial class Default2 : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
}
protected void btnContent_Click(object sender, EventArgs e)
...{
Label lbl = this.Master.FindControl("lblMaster") as Label;
Response.Write("<script language='javascript'>alert('" + lbl.Text + "');</script>");
}
}