子页访问母版页中的控件
方法一:FindControl方法
(Master.FindControl("txt1") as TextBox).Text
方法二:MasterType方法
母版页中有一个ID=MasterLabel的TextBox,添加代码如下:
<script runat="server">
private TextBox masterLabel;
public TextBox MasterLabel
{
get { return masterLabel; }
set { masterLabel = value; }
}
</script>
子页面中有一个ID=Label1的Label,添加代码如下:
<%@ MasterType VirtualPath="~/MasterPage.master" %>
<script runat="server">
new void Page_Load(Object sender, EventArgs e)
{
Label1.Text = Master.MasterLabel.Text;
}
</script>
个人认为,第一个方法更为灵活,可以使用任意控件;第二个方法比较麻烦,使用时必须先公开声明该控件,然后才能使用