Master page variable - access on content page

  • Thread starter Thread starter Matty J
  • Start date Start date
M

Matty J

Hi.

I have:
- a master page base class
- a master page (class) which inherits from the master page base class

- a content page base class
- a content page (class) which inherits from the content page base
class.

The master page base class has a function in it which sets the value
of an object after doing a request to the database (based on domain
name).

I then want the content page (and controls on it) to be able to access
the value (so the website template / master page can be set based on
domain name).

I'm having trouble getting the correct order of events, including the
event placement of the function in the master page, so that the
content page can access the set variable.

If anyone can help me with the events in each page that'd be greatly
appreciated - currently I'm just guessing and not getting very far. I
looked at this article but I haven't been able to ascertain quite the
correct order in the 4 different classes (and where and when I should
be overriding events and/or calling events in the base class eg.
base.Page_PreInit etc.): http://msdn.microsoft.com/en-us/library/dct97kc3.aspx

Cheers
Matt
 
To do what you want, make the variable in the Master Page Public. If you
want to set this variable from the Content Page, you must do it in the
Content.PreInit event. You should be able to access it anywhere in the
Content Page (although what value it will have will depend on when you set
it and when you access it). To do this, use Me.Master.yourproperty. However,
in order to do this, you will need to include the @MasterType directive in
your Content Page so that the property is available. Sometimes it takes some
adjusting to how and when you should use Me.Master from your Content Page,
but I'm sure you'll adjust pretty quickly.
 
Excellent, thanks Nathan.

The problem I'm having is that I want the variable set in the *Master*
page base class. Setting the variable in the Master Base Class
Page_PreInit event doesn't seem to happen before the Content Page Base
Class PreInit event, as casting the Master Page Base Class's public
variable in the Content Page Base Class only returns null...?

Also, 'Me.' does not seem to be working, do you mean 'this.'?

Cheers
Matt
 
Yes, I did mean this (Me is VB.NET's equivelant of C#'s this). I think I may
need to see your code in order to help you more. PreInit is the first event
that happens in the Content page. Please post your code so that I can see
exactly what you are doing, it may make it easier for me to help you come up
with a solution.
 
Hi Nathan, and thanks again for your reply.

I managed to get it work, primarily by going back to basics and
building up from there, using your advice etc., so that I didn't have
so many variables affecting it.

These two articles were particularly good help:
http://www.odetocode.com/articles/450.aspx
http://www.asp.net/learn/master-pages/tutorial-06-cs.aspx

I think in the end I was using events such as BasePage_PreInit as per
in the first example, thinking that it was an actual .NET event when
it wasn't, so it actually needed to be added to the PreInit event.

Do you know of any actual list of the exact event names of master/
content page load?

Cheers
Matt
 
Back
Top