1

I am using nested master pages where i want to use Label control from nested master page and update its text. but it is not accessing. When i removed outer master page then it is working fine. Following is the markup and code.

OUTER MASTER

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Roster.Site" %>

NESTED MASTER

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="RoasterMaster.master.cs" Inherits="Roster.RoasterMaster"  MasterPageFile="~/Site.Master" %>
<%@ MasterType VirtualPath ="~/Site.Master" %>

CONTENT PAGE

<%@ Page Language="C#" AutoEventWireup="true" Inherits="RequestsView" CodeBehind="ViewRequestsByPM.aspx.cs" MasterPageFile ="~/Roaster/RoasterMaster.Master" Title ="Roaster- View Requests by PM" %>
<%@ MasterType VirtualPath ="~/Roaster/RoasterMaster.Master" %>

CONTENT PAGE CODE

protected void Page_Load(object sender, EventArgs e)
{


    Label lblTitle = new Label();

    lblTitle =(Label)Master.FindControl("lblTitle");
    lblTitle.Text = "View Roaster Request";
}

What is going wrong with the implementation. Please help. Thanks

4

2 回答 2

2

您可以添加以下代码片段

嵌套母版页

public string PageTitle { get; set; } // In page_load
lblTitle.Text = PageTitle;

内容页码

this.Master.PageTitle = "YOUR TEXT";

这将为您工作...

于 2012-12-06T11:46:26.660 回答
1

假设您的标签在名册母版页中,您可以简单地在母版页代码后面添加方法来设置文本。例如,

RoasterMaster.master.cs

public void SetTitle(string value)
{
   this.lblTitle = value;
}

并在内容页面代码中

Master.SetTitle("View Roaster Request");

如果您的标签在外部主机中,那么您可以类似地将呼叫从名册主机代码转发到外部主机。

编辑
您的代码在嵌套的主案例场景中不起作用,因为主页面内容被添加到具有不同命名容器的页面控件层次结构中。FindControl方法不跨越多个命名容器,这里就是这种情况 - 因为嵌套你有嵌套的命名容器。Page.Master会给你外部命名容器,但你的标签可能位于内部命名容器中。一种方法是编写您自己的查找控件实现,该实现将在控件树中重复出现,但实际上它没有意义——我宁愿使用上面更有效且更重要的是可维护性更好的代码。

于 2012-12-06T10:51:30.467 回答