How to access a user control in a masterpage from a content page?

You can use two methods. The first is by using Page.Master. FindControl('controlID') .

Then you can cast it to the type of your user control. The second method is by adding a %@ MasterType VirtualPath="" TypeName=""% tag to your aspx page. In the VirtualPath add the virtual path to the master page, and the class in the TypeName .

You can then access everything with intellisense.

Up vote 2 down vote favorite 2 share g+ share fb share tw.

Lets say that I have a header user control in a master page, and want to change a property of the user control depending on what content page is loaded inside of the master page. How might I go about this? Thanks!

C# asp.net usercontrols master-pages link|improve this question edited Dec 19 '08 at 23:47 asked Dec 19 '08 at 22:05Bryan Denny7,548114478 80% accept rate.

You can use two methods. The first is by using Page.Master. FindControl('controlID').

Then you can cast it to the type of your user control. The second method is by adding a tag to your aspx page. In the VirtualPath add the virtual path to the master page, and the class in the TypeName.

You can then access everything with intellisense.

There's one other method, and that's by making a public property on the master page that exposes the user control.

Using a public property would work. In the content page's FormLoad method, you could do something like this (VB): Dim myMaster as MyMasterPage = CType(Page. Master, MyMasterPage) myMaster.MyUserControl.

Text = "Hello!

Using code-behind files is not a best practice. Here is attempts to get rid of them at all haacked.com/archive/2008/12/19/… – horseman Dec 21 '08 at 17:35 How is it not a best practice? You're confusing ASP.

NET webforms, which this question is about and ASP. NET MVC. This answer is perfectly suitable.

– jwalkerjr Dec 22 '08 at 5:19.

This is also a good link about accessing objects inside of the MasterPage.

First find the user control in the masterpage as below. Then find the control you need to access their property. UserControl uch = Page.Master.

FindControl("ucHeader1") as UserControl; PlaceHolder phProxylist= ucHeader1. FindControl("phProxy") as PlaceHolder; DropDownList ddlproxylist1 = ucHeader1. FindControl("ddlProxyList") as DropDownList; phProxylist.

Visible = false; Hope this helps.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions