Get reference to Master Page type

P

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
 
?

=?iso-8859-1?Q?Gabriel_Maga=F1a?=

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 {
 
T

Teemu Keiski

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?
 
P

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
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 {
 
P

PJ

Yes, this works in pages that use the master pages, but not for user
controls. See above post.
 
G

Guest

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.
 
?

=?iso-8859-1?Q?Gabriel_Maga=F1a?=

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...
 
T

Teemu Keiski

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/accessing_codebehind_classes_in_directories_in_asp_net_2_0.aspx
http://odetocode.com/Blogs/scott/archive/2005/09/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/UnderstandingCodeBehind/UnderstandingCodeBehind.aspx
 
Joined
Jun 18, 2010
Messages
3
Reaction score
0
Hi I asnwered this question in another thread, but i am new to this forum, so if someone could tell me how to access the posts i made. sorry for the stupid question, but i am sure we can remove the post when i got the answer...(and replace it with a reference to the post i made..)

Sharals
 
Joined
Jun 18, 2010
Messages
3
Reaction score
0
Hi, (this seems related..)

To prevent repeating myself in code i wonder if i should add a constructor to a webform (instead of assigning values in pageload), or i guess it is no use as the object is lost every time the page loads ?
Any idea's ?

Kind Regards,

Hugo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top