问题
I have a label named headerLabel
in my master page, and I'd like to set its text to the title from the content page. How do I do this?
回答1:
On your master page create a public property - something along the lines of:
public string LabelValue
{
get{ return this.headerLabel.Text;}
set{ this.headerLabel.Text = value;}
}
Then, on your child page you can do this:
((MyMasterPage)this.Master).LabelValue = "SomeValue";
回答2:
You need to find control by it's id on the content page then set text property of label like this
(Label)MasterPage.FindControl("headerLabel").Text="Your Title";
it better to check null before assigning the text property like this
Label mylbl= (Label) MasterPage.FindControl("headerLabel");
if(mylbl!= null)
{
mylbl.Text = "Your Title";
}
来源:https://stackoverflow.com/questions/21160132/asp-net-passing-data-from-content-page-to-master-page