Tuesday, June 17, 2008

Access master page members from a page

download the source code

Sometimes we need define a property or a method in a masterpage and we need access to that property or method in pages that are using that masterpage. To do this we have two choices.

1- Using FindControl (this.Master.FindControl("usercontrolid"))

2- Using this.Master.YourProperty

The first one is not type safe so you could misspelled a property name then you will get null so this approach is error porn. Furthermore, the control should be cast to a valid control. Also you cannot access property by this approach but you can access to controls in masterpage. However, it does not need any other effort.

The second one is type safe, access to masterpage properties is possible and there is no need for casting. However, it needs that the control be defined at least internal and you need define the master type in your page. Then compiler can detect that master page property and it allows you to access to those property through intelisence.

Let's see an example: we have a Label and image in our masterpage and we have some methods in our master page. We need to call these methods from page that is using these masterpage. Also we have a property in master page that we need to have access in our page. Take a look at this:

This is the master page html code:

This master page properties and methods

public Label TitleLable

{

get

{

return LabelTitle;

}

}

public void ShowImage()

{

Image1.Visible = true;

}

public void HideImage()

{

Image1.Visible = false;

}

Then we create a page that is using this masterpage. And this is the html code:

Important: in this code we defined a mastertype directive to notify compiler that we are using these masterpages then if you compile and try to access masterpage methods and properties you can see them.

Take a look to the code behind of the page. As you see we implemented both way. I highly recommend the second way. Remember the first way does not force you to define mastertype in the page. While, second way need the mastertype directive in html code of the page:

protected void ButtonChangeTitle_Click(object sender, EventArgs e)

{

//first way

//Label lbl = Master.FindControl("LabelTitle") as Label;

//lbl.Text = "New Title is Assigned";

//second way

Master.TitleLable.Text = "New Title is Assigned";

}

protected void ButtonHideImage_Click(object sender, EventArgs e)

{

//first way

//Image img = Master.FindControl("Image1") as Image;

//img.Visible = false;

//second way

Master.HideImage();

}

protected void ButtonShowImage_Click(object sender, EventArgs e)

{

//first way

//Image img = Master.FindControl("Image1") as Image;

//img.Visible = true;

//second way

Master.ShowImage();

}

As I said before I highly recommend the second way. Let's review the steps:

1- Create a master page

2- Define some public properties and methods in masterpage

3- Create a page using the master page

4- Define the mastertype directive in page

5- You can access masterpage public properties and method through Master object in the page

4 comments:

waqas said...

Extremely help full, solved my problem I had for days..
thanks very much indeed.

Anonymous said...

Thanks for the help. Good post!

Chris

Leandro Oscar said...

Thanks a lot.
You've saved me!!!

Leandro Nuñez from Argentina.

Anonymous said...

Thanx Dude.
That Code Really Helped Me a Lot