ASP.NET:将数据从内容页面传递到母版页[重复]

时间:2022-01-23 15:55:14

This question already has an answer here:

这个问题在这里已有答案:

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?

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

2 个解决方案

#1


3  

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


2  

You need to find control by it's id on the content page then set text property of label like this

您需要在内容页面上通过它的id找到控件,然后像这样设置标签的text属性

(Label)MasterPage.FindControl("headerLabel").Text="Your Title";

it better to check null before assigning the text property like this

最好在分配像这样的text属性之前检查null

 Label mylbl= (Label) MasterPage.FindControl("headerLabel");
    if(mylbl!= null)
    {
        mylbl.Text = "Your Title";
    }

#1


3  

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


2  

You need to find control by it's id on the content page then set text property of label like this

您需要在内容页面上通过它的id找到控件,然后像这样设置标签的text属性

(Label)MasterPage.FindControl("headerLabel").Text="Your Title";

it better to check null before assigning the text property like this

最好在分配像这样的text属性之前检查null

 Label mylbl= (Label) MasterPage.FindControl("headerLabel");
    if(mylbl!= null)
    {
        mylbl.Text = "Your Title";
    }