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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1149680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:25:12  来源:igfitidea点击:

Can I change the text of a label in a masterpage when loading a content page?

c#asp.netvb.net

提问by Cunners

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

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

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?

这可能吗?

采纳答案by Cunners

Yes.

是的。

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

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

回答by CD..

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

您可以在母版页中创建一个公共属性来更改标签。

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

}

回答by Adrian Godong

Yes, it is possible. MasterPagebehaves just like UserControlin 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.:

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

  3. Call the method defined in step 1 to change the MasterPagecontents.
  1. 在 MasterPage 上创建一个属性或方法,使您能够对标签进行更改。例如:

    public void ChangeLabel(string label) {
      lblHeading.Text = label;
    }
    
  2. 从您的 中PageMasterPage使用Page.Master属性获取对 的引用。

  3. 调用步骤 1 中定义的方法来更改MasterPage内容。

Additional info: You may need to cast Page.Masterinto your MasterPagetype, try Coding the Wheel's linkfor instructions on how to do that.

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

回答by Muhammad Akhtar

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

是的,您可以通过这种非常简单的方式......

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

回答by Gary Huckabone

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!";
  }

回答by Fandango68

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...

它还取决于您在母版页中的控件的深度。就我而言,我在 ContentPlaceHolder 控件中有一个 Label 控件......所以我必须这样做......

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