加载内容页面时,我可以更改母版页中标签的文字吗?

时间:2021-07-12 10:49:04

I have a label in a master page (sample.master) called lblHeading.

我在一个名为lblHeading的母版页(sample.master)中有一个标签。

I want to dynamically change the text of the label when I load the content page.

我想在加载内容页面时动态更改标签的文本。

I need to do this because I want to change the heading to something meaningful but only after I know about the content of the page.

我需要这样做,因为我想将标题改为有意义的东西,但只有在我知道页面的内容之后。

Is this possible?

这可能吗?

6 个解决方案

#1


6  

Yes.

You want to create a strongly-type master page and you can then access it's properties from your content page during Page_Load or wherever else.

您想要创建一个强类型母版页,然后您可以在Page_Load或其他任何地方从内容页面访问它的属性。

#2


13  

yes, you can in this very simple way........

是的,你可以用这种非常简单的方式........

((Label)Master.FindControl("lblHeading")).Text = "your new text";

#3


3  

Yes, it is possible. MasterPage behaves just like UserControl in your page.

对的,这是可能的。 MasterPage的行为与您页面中的UserControl类似。

Possible steps to implement this:

实现此目的的可能步骤:

  1. Create a property or method on the MasterPage that enables you to make changes to the Label. E.g.:

    在MasterPage上创建一个属性或方法,使您可以对Label进行更改。例如。:

    public void ChangeLabel(string label) {
      lblHeading.Text = label;
    }
    
  2. From your Page, get the reference to the MasterPage by using the Page.Master property.

    从您的页面,使用Page.Master属性获取对MasterPage的引用。

  3. Call the method defined in step 1 to change the MasterPage contents.
  4. 调用步骤1中定义的方法来更改MasterPage内容。

Additional info: You may need to cast Page.Master into your MasterPage type, try Coding the Wheel's link for instructions on how to do that.

附加信息:您可能需要将Page.Master转换为MasterPage类型,尝试编码Wheel的链接以获取有关如何执行此操作的说明。

#4


2  

You can create a public property in the masterpage that will change the label.

您可以在母版页中创建将更改标签的公共属性。

public string Heading
{
    set 
    {
        lblHeading.text = value;
    }

}

#5


2  

Do as stated above. Then, e.g., from your page do this (master page has label with ID="Label_welcome"):

如上所述。然后,例如,从您的页面执行此操作(母版页具有ID =“Label_welcome”的标签):

Label mpLabel = (Label)Page.Master.FindControl("Label_welcome");

  if (mpLabel != null)
  {
    mpLabel.Text = "Welcome Fazio!";
  }

#6


0  

It also depends on how deep your controls inside the Master page are. In my case, I had a Label control inside a ContentPlaceHolder control... so I had to do this...

它还取决于Master页面内控件的深度。就我而言,我在ContentPlaceHolder控件中有一个Label控件......所以我不得不这样做......

Label myLBL = this.Master.FindControl("HeaderContent").FindControl("myLabel") as Label; 

#1


6  

Yes.

You want to create a strongly-type master page and you can then access it's properties from your content page during Page_Load or wherever else.

您想要创建一个强类型母版页,然后您可以在Page_Load或其他任何地方从内容页面访问它的属性。

#2


13  

yes, you can in this very simple way........

是的,你可以用这种非常简单的方式........

((Label)Master.FindControl("lblHeading")).Text = "your new text";

#3


3  

Yes, it is possible. MasterPage behaves just like UserControl in your page.

对的,这是可能的。 MasterPage的行为与您页面中的UserControl类似。

Possible steps to implement this:

实现此目的的可能步骤:

  1. Create a property or method on the MasterPage that enables you to make changes to the Label. E.g.:

    在MasterPage上创建一个属性或方法,使您可以对Label进行更改。例如。:

    public void ChangeLabel(string label) {
      lblHeading.Text = label;
    }
    
  2. From your Page, get the reference to the MasterPage by using the Page.Master property.

    从您的页面,使用Page.Master属性获取对MasterPage的引用。

  3. Call the method defined in step 1 to change the MasterPage contents.
  4. 调用步骤1中定义的方法来更改MasterPage内容。

Additional info: You may need to cast Page.Master into your MasterPage type, try Coding the Wheel's link for instructions on how to do that.

附加信息:您可能需要将Page.Master转换为MasterPage类型,尝试编码Wheel的链接以获取有关如何执行此操作的说明。

#4


2  

You can create a public property in the masterpage that will change the label.

您可以在母版页中创建将更改标签的公共属性。

public string Heading
{
    set 
    {
        lblHeading.text = value;
    }

}

#5


2  

Do as stated above. Then, e.g., from your page do this (master page has label with ID="Label_welcome"):

如上所述。然后,例如,从您的页面执行此操作(母版页具有ID =“Label_welcome”的标签):

Label mpLabel = (Label)Page.Master.FindControl("Label_welcome");

  if (mpLabel != null)
  {
    mpLabel.Text = "Welcome Fazio!";
  }

#6


0  

It also depends on how deep your controls inside the Master page are. In my case, I had a Label control inside a ContentPlaceHolder control... so I had to do this...

它还取决于Master页面内控件的深度。就我而言,我在ContentPlaceHolder控件中有一个Label控件......所以我不得不这样做......

Label myLBL = this.Master.FindControl("HeaderContent").FindControl("myLabel") as Label;