2017-02-15 80 views
0

我需要根据某些逻辑更改母版页的子页面的页眉的文本。下面是我的母版页代码:从母版页更改子页面的文本

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

在孩子的网页我有下面的代码:

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

我怎样才能到任何其他的母版页更改文本的价值?

回答

1

您可以搜索MasterPage中的控件。如果直接在你的的ContentPlaceHolder喜欢你的例子设置文本,该文本将在LiteralControl:

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

或者,如果你想搜索的页面控件集合在一个控制:

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

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