access master page from ascx control

963 views Asked by At

I have a public property in a master page. I want to access this property from an ascx user control.

My master page is named master_public and it's in a namespace called "master".

So I tried writing:

dim m=ctype(page.master,master.master_public)
dim foobar=m.foobar

The intellisense says that master.master_public doesn't exist.

I tried removing the namespace from the master page. Still no luck.

The master page is declared "partial public class". But this name doesn't seem to be recognized.

Answers here Accessing Master page control in ascx file seem to imply that this should just ... work. Is there some attribute or setting or something I have to include to make master pages accessible as class types?

1

There are 1 answers

0
ajakblackgoat On

Add the following on top of your form (.aspx):

<%@ MasterType TypeName="master.master_public" %>

The above directive will expose public members of the masterpage to the form. To access your property from the form, simply reference as below:

Me.Master.YourProperty

Therefore, in order to access the masterpage public property from user controls added to the form, just cast the master page object :

CType(Me.Page.Master, master.master_public).YourProperty