By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 2,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

Get reference to Master Page type

P: n/a
PJ
How can I get a reference to the master page class? It is defined as a
partial class, but I cannot seem to type a variable to the name of the
partial class? The compiler continually shows "The type 'MyMaster' is not
defined."

I would like to reference the master page from a control, by casting the
Master property of the Page property to the type, but obviously I cannot do
this if I can't get a reference to the actual type.

Thanks,
~PJ

Jan 4 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Here's a code snipped from my app:

master_AppResMortApp master = (MasterType)this.Master;

Where "MasterType" is the type defined in the master page source:
public class MasterType : MasterPage {
"PJ" <pj******@hotmail.com> wrote in message news:Qq******************************@speakeasy.ne t...
How can I get a reference to the master page class? It is defined as a
partial class, but I cannot seem to type a variable to the name of the
partial class? The compiler continually shows "The type 'MyMaster' is not
defined."

I would like to reference the master page from a control, by casting the
Master property of the Page property to the type, but obviously I cannot do
this if I can't get a reference to the actual type.

Thanks,
~PJ


Jan 4 '06 #2

P: n/a
Look at this article from the MSDN:
http://msdn2.microsoft.com/en-us/library/c8y19k6h.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"PJ" wrote:
How can I get a reference to the master page class? It is defined as a
partial class, but I cannot seem to type a variable to the name of the
partial class? The compiler continually shows "The type 'MyMaster' is not
defined."

I would like to reference the master page from a control, by casting the
Master property of the Page property to the type, but obviously I cannot do
this if I can't get a reference to the actual type.

Thanks,
~PJ

Jan 4 '06 #3

P: n/a
Hi,

Could using @MasterType directive on that page solve the problem? Probably,
but it would also make you cast Page property to the type of the Page (so
that Master property is typed), which would seriously harm the reusability
of your control.

@MasterType
http://msdn2.microsoft.com/en-us/library/ms228274.aspx

So, any chance that what the control is actually looking for from the master
page, is actually delivered to it via property, so that it wouldn't be tied
to a specific master page or just a specific Page?

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"PJ" <pj******@hotmail.com> wrote in message
news:Qq******************************@speakeasy.ne t...
How can I get a reference to the master page class? It is defined as a
partial class, but I cannot seem to type a variable to the name of the
partial class? The compiler continually shows "The type 'MyMaster' is not
defined."

I would like to reference the master page from a control, by casting the
Master property of the Page property to the type, but obviously I cannot
do
this if I can't get a reference to the actual type.

Thanks,
~PJ

Jan 4 '06 #4

P: n/a
PJ
When you add a master page to an app it creates it as a partial class. Yours is not.

I am trying to get a reference to the master page type from the a class within the app_code directory. Apparently, none of the partial classes created by the aspx pages, master pages, and controls are available to classes in the app_code directory. asp.net does not let you add master pages to the app_code directory.

So it seems as if you must create a class that your master page will inherit from, which is perhaps what you have done? Also, implementing an interface is another option, which I have done.

Someone please correct me if I'm wrong or feel free to add to this.

~PJ
"Gabriel Magaña" <no*****@no-spam.com> wrote in message news:u1**************@TK2MSFTNGP14.phx.gbl...
Here's a code snipped from my app:

master_AppResMortApp master = (MasterType)this.Master;

Where "MasterType" is the type defined in the master page source:
public class MasterType : MasterPage {
"PJ" <pj******@hotmail.com> wrote in message news:Qq******************************@speakeasy.ne t...
How can I get a reference to the master page class? It is defined as a
partial class, but I cannot seem to type a variable to the name of the
partial class? The compiler continually shows "The type 'MyMaster' is not
defined."

I would like to reference the master page from a control, by casting the
Master property of the Page property to the type, but obviously I cannot do
this if I can't get a reference to the actual type.

Thanks,
~PJ


Jan 4 '06 #5

P: n/a
PJ
Yes, this works in pages that use the master pages, but not for user
controls. See above post.

"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:Or**************@TK2MSFTNGP10.phx.gbl...
Hi,

Could using @MasterType directive on that page solve the problem? Probably, but it would also make you cast Page property to the type of the Page (so
that Master property is typed), which would seriously harm the reusability
of your control.

@MasterType
http://msdn2.microsoft.com/en-us/library/ms228274.aspx

So, any chance that what the control is actually looking for from the master page, is actually delivered to it via property, so that it wouldn't be tied to a specific master page or just a specific Page?

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"PJ" <pj******@hotmail.com> wrote in message
news:Qq******************************@speakeasy.ne t...
How can I get a reference to the master page class? It is defined as a
partial class, but I cannot seem to type a variable to the name of the
partial class? The compiler continually shows "The type 'MyMaster' is not defined."

I would like to reference the master page from a control, by casting the
Master property of the Page property to the type, but obviously I cannot
do
this if I can't get a reference to the actual type.

Thanks,
~PJ


Jan 4 '06 #6

P: n/a
If you want a control to manipulate the layout of the pages in your
application then instead of attempting to directly access the masterpage
members why do not you program the control to raise events to be handled by
the webform that will contain the control. The webform can thereafter access
the controls and members of the master page and change them based on the
criteria that you passed through the eventargs. If you have any further
questions on how to create events and event arguments, let me know.

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"PJ" wrote:
When you add a master page to an app it creates it as a partial class. Yours is not.

I am trying to get a reference to the master page type from the a class within the app_code directory. Apparently, none of the partial classes created by the aspx pages, master pages, and controls are available to classes in the app_code directory. asp.net does not let you add master pages to the app_code directory.

So it seems as if you must create a class that your master page will inherit from, which is perhaps what you have done? Also, implementing an interface is another option, which I have done.

Someone please correct me if I'm wrong or feel free to add to this.

~PJ
"Gabriel Magaña" <no*****@no-spam.com> wrote in message news:u1**************@TK2MSFTNGP14.phx.gbl...
Here's a code snipped from my app:

master_AppResMortApp master = (MasterType)this.Master;

Where "MasterType" is the type defined in the master page source:
public class MasterType : MasterPage {
"PJ" <pj******@hotmail.com> wrote in message news:Qq******************************@speakeasy.ne t...
> How can I get a reference to the master page class? It is defined as a
> partial class, but I cannot seem to type a variable to the name of the
> partial class? The compiler continually shows "The type 'MyMaster' is not
> defined."
>
> I would like to reference the master page from a control, by casting the
> Master property of the Page property to the type, but obviously I cannot do
> this if I can't get a reference to the actual type.
>
> Thanks,
> ~PJ
>
>
>

Jan 4 '06 #7

P: n/a
So it seems as if you must create a class that your master page will inherit from, which is perhaps what you >have done? Also, implementing an interface is another option, which I have done.


Yes this is the code I call from a page that uses a master page. Sorry if I misunderstood your question, I have never needed to tackle the same problem as you...
Jan 4 '06 #8

P: n/a
Note what I said about having properties on the control being set by the
page (or having events as was said), instead of trying to access the Page &
Master page from the control in a typed manner (plus that they indeed are
not available, since in VS2005 page codebehind are compiled last, unless you
add the base class of your page to app_code). It reduces reusability of your
control if you tie it to a specific (master) page.

Something like discussed here:
http://www.csharper.net/blog/accessi...p_net_2_0.aspx
http://odetocode.com/Blogs/scott/arc...9/12/2186.aspx
http://west-wind.com/weblog/posts/3016.aspx

Anyways, I'd think that the new Web Application project might give a
solution to this since it changes the VS2005 to have VS2003 type of model
(all pre-built into a single assembly, just as it is with VS2003)
http://webproject.scottgu.com

Specifically
http://webproject.scottgu.com/CSharp...odeBehind.aspx

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"PJ" <pj******@hotmail.com> wrote in message
news:9c********************@speakeasy.net...
Yes, this works in pages that use the master pages, but not for user
controls. See above post.

"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:Or**************@TK2MSFTNGP10.phx.gbl...
Hi,

Could using @MasterType directive on that page solve the problem?

Probably,
but it would also make you cast Page property to the type of the Page (so
that Master property is typed), which would seriously harm the
reusability
of your control.

@MasterType
http://msdn2.microsoft.com/en-us/library/ms228274.aspx

So, any chance that what the control is actually looking for from the

master
page, is actually delivered to it via property, so that it wouldn't be

tied
to a specific master page or just a specific Page?

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"PJ" <pj******@hotmail.com> wrote in message
news:Qq******************************@speakeasy.ne t...
> How can I get a reference to the master page class? It is defined as a
> partial class, but I cannot seem to type a variable to the name of the
> partial class? The compiler continually shows "The type 'MyMaster' is not > defined."
>
> I would like to reference the master page from a control, by casting
> the
> Master property of the Page property to the type, but obviously I
> cannot
> do
> this if I can't get a reference to the actual type.
>
> Thanks,
> ~PJ
>
>
>



Jan 4 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.