ASP.NET:将数据从内容页传递到母版页

Phi*_*hil 2 asp.net master-pages

我的headerLabel母版页中有一个标签,我想将其文本设置为内容页的标题。我该怎么做呢?

Lia*_*ath 5

在您的母版页上创建一个公共属性 - 类似于:

public string LabelValue
{
  get{ return this.headerLabel.Text;}
  set{ this.headerLabel.Text = value;}
}
Run Code Online (Sandbox Code Playgroud)

然后,在您的子页面上,您可以执行以下操作:

((MyMasterPage)this.Master).LabelValue = "SomeValue";
Run Code Online (Sandbox Code Playgroud)

  • 您必须提供返回此标签的公共属性 `headerlabel`。那么这种方法比 [`FindControl` 方法](http://stackoverflow.com/a/21160299/284240) 更好。否则您无法直接访问它,因为默认情况下控件是“受保护的”。 (2认同)