获取内容页面中的母版页对象的ID

时间:2022-06-03 21:13:47

If the master page has a label with the id label1 how do I control that id in the content page. The id is not passed down so i can't control it inherently. For example if i have a control with the id contentLabel i can access it code by just typing contentLabel.(whatever i'm doing)

如果母版页的标签带有id label1,我该如何在内容页面中控制该id。 id不会传递下来所以我无法控制它本身。例如,如果我有一个id contentLabel的控件,我可以通过输入contentLabel来访问它代码。(无论我在做什么)

1 个解决方案

#1


10  

Here are two options:

这有两个选择:

1: make sure your content aspx specifies MasterType:    

1:确保您的内容aspx指定MasterType:

<%@ MasterType VirtualPath="~/yourMasterPageName.master" %>

Doing this lets your content page know what to expect from your master-page and gives you intellisense. So, now you can go ahead and expose the label's Text property on the master page's code-behind.

这样做可以让您的内容页面知道主页面的内容,并为您提供智能感知。所以,现在你可以继续在主页的代码隐藏中公开标签的Text属性。

public string ContentLabelText
{
    get { return contentLabel.Text; }
    set { contentLabel.Text = value; }
}

Then you can access it in your content page's code-behind page ala:

然后,您可以在内容页面的代码隐藏页面ala中访问它:

Master.ContentLabelText = "hah!";

or, 2: You can access the label via FindControl() like so:

或者,2:您可以通过FindControl()访问标签,如下所示:

var contentLabel = Master.FindControl("contentLabel") as Label;

#1


10  

Here are two options:

这有两个选择:

1: make sure your content aspx specifies MasterType:    

1:确保您的内容aspx指定MasterType:

<%@ MasterType VirtualPath="~/yourMasterPageName.master" %>

Doing this lets your content page know what to expect from your master-page and gives you intellisense. So, now you can go ahead and expose the label's Text property on the master page's code-behind.

这样做可以让您的内容页面知道主页面的内容,并为您提供智能感知。所以,现在你可以继续在主页的代码隐藏中公开标签的Text属性。

public string ContentLabelText
{
    get { return contentLabel.Text; }
    set { contentLabel.Text = value; }
}

Then you can access it in your content page's code-behind page ala:

然后,您可以在内容页面的代码隐藏页面ala中访问它:

Master.ContentLabelText = "hah!";

or, 2: You can access the label via FindControl() like so:

或者,2:您可以通过FindControl()访问标签,如下所示:

var contentLabel = Master.FindControl("contentLabel") as Label;

相关文章