【发布时间】:2014-01-16 11:12:11
【问题描述】:
我的母版页中有一个名为headerLabel 的标签,我想将其文本设置为内容页的标题。我该怎么做?
【问题讨论】:
-
你可以使用this question
标签: asp.net master-pages
我的母版页中有一个名为headerLabel 的标签,我想将其文本设置为内容页的标题。我该怎么做?
【问题讨论】:
标签: asp.net master-pages
在您的母版页上创建一个公共属性 - 类似于:
public string LabelValue
{
get{ return this.headerLabel.Text;}
set{ this.headerLabel.Text = value;}
}
然后,在您的子页面上,您可以这样做:
((MyMasterPage)this.Master).LabelValue = "SomeValue";
【讨论】:
headerlabel,它会返回此标签。那么这种方法比FindControl approach更好。否则你不能直接访问它,因为控件默认是protected。
您需要在内容页面上通过它的 id 找到控件,然后像这样设置标签的文本属性
(Label)MasterPage.FindControl("headerLabel").Text="Your Title";
最好在像这样分配文本属性之前检查 null
Label mylbl= (Label) MasterPage.FindControl("headerLabel");
if(mylbl!= null)
{
mylbl.Text = "Your Title";
}
【讨论】: