简体   繁体   English

从母版页更改子页的文本

[英]Change text of child page from master page

I need to change text of header of child page from master page depending upon some logic. 我需要根据某些逻辑从母版页更改子页头的文本。 Below is my code in master page: 以下是我在母版页中的代码:

<div id="content-header">
    <h1>
        <asp:ContentPlaceHolder ID="cphHead" runat="server"></asp:ContentPlaceHolder>
    </h1>
</div>

In child page I have below code: 在子页面中,我有以下代码:

<asp:Content ID="Content3" ContentPlaceHolderID="cphHead" runat="Server">
  TEXT
</asp:Content>

How can I change value of TEXT to any other from master page? 如何从母版页将TEXT的值更改为其他值?

You can search controls in the MasterPage. 您可以在MasterPage中搜索控件。 If you set text directly in your ContentPlaceHolder like your example, the text will be in a LiteralControl: 如果像示例一样直接在ContentPlaceHolder中设置文本,则文本将在LiteralControl中:

((LiteralControl)this.FindControl("cphHead").Controls[0]).Text = "Change TEXT";

Or if you want search to a control in the page control collection: 或者,如果要搜索页面控件集合中的控件,请执行以下操作:

ControlCollection controls = this.FindControl("cphHead").Controls;

foreach (Control control in controls)
{
    if (control.GetType() == typeof(LiteralControl))
    {
        ((LiteralControl)control).Text = "Change TEXT";
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM