ASP.NET: Passing data from content page to master page [duplicate]

南楼画角 提交于 2019-12-11 06:45:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!