Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ID of Master Page object in Content Page

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)

like image 679
auwall12688 Avatar asked Jun 21 '12 20:06

auwall12688


People also ask

How can you reference a control on the master page from the content page?

To reference a control on the master pageUse the FindControl method, using the value returned by the Master property as the naming container. The following code example shows how to use the FindControl method to get a reference to two controls on the master page, a TextBox control and a Label control.

How do you access a public property defined in a master page from the content page?

To access members of a specific master page from a content page, you can create a strongly typed reference to the master page by creating a @ MasterType directive. The directive allows you to point to a specific master page. When the page creates its Master property, the property is typed to the referenced master page.


1 Answers

Here are two options:

1: make sure your content aspx specifies 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.

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:

Master.ContentLabelText = "hah!";

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

var contentLabel = Master.FindControl("contentLabel") as Label;
like image 106
canon Avatar answered Oct 24 '22 19:10

canon