Master Page custom property always null :(

  • Thread starter Thread starter fishek
  • Start date Start date
F

fishek

I have a Master Page with a property defined in codebehind like this:

private string profile;

public string Profile {
get { return profile; }
}

I can set the property value in the master page fine (normally
page_load or page_preinit), i.e.:

profile = "custom";

In the page that uses the Master page, I can see the property like
this:

string test = Master.Profile;

But, The property is ALWAYS null. It never saves the value I give it
in the Master Page codebehind. Is there any way around this issue? I
would like to pre-initialize some common properties without having to
copy the code into each aspx page. Thanks for the help!

Kyle
 
Consider using AppSettings, they are quite easy to use, and easy to set.
http://msdn2.microsoft.com/en-us/library/ms228154(VS.80).aspx

web.config:
<appSettings>
<add key="myvar" value="test1234" />
</appSettings>

code:
string myvar = ConfigurationManager.AppSettings["myvar"]


or if it is a per-user setting, the Profile / ProfileManager could be the
answer
http://msdn2.microsoft.com/en-us/library/system.web.profile(VS.80).aspx
A ready to use "SqlProfileProvider" is provided with 2.0 out-of-the-box
Just make a database, prep it with the aspnet_regsql.exe, and setup the
connectionstring.
 
Unfortunately, we do not use SQL server and I cannot create new DB
tables at this point.

There were a few things I wanted to do in the Master page that I could
reference in the other pages:
1. Read and store cookie + settings
2. Initialize webservice
3. Save form factor information.

Unfortunately, any properties I create in the Master are always null in
the client page unless I set them explicitly in the client page. I can
access properties of controls on the page, but using controls would be
a pretty bad solution.

Am I right in thinking that custom Master Page properties that have
values set in the Master Page code behind should be accessible in the
client pages? Is there some Asp.net setting I have to set somewhere
for this to work?

Thanks for the help,
Kyle
 
There were a few things I wanted to do in the Master page that I could
reference in the other pages:
1. Read and store cookie + settings
2. Initialize webservice
3. Save form factor information.

Then I would either make a httpModule, or consider using session ... the
masterpage is not the place to store this stuff

Unfortunately, any properties I create in the Master are always null in
the client page unless I set them explicitly in the client page. I can
access properties of controls on the page, but using controls would be
a pretty bad solution.

Am I right in thinking that custom Master Page properties that have
values set in the Master Page code behind should be accessible in the
client pages? Is there some Asp.net setting I have to set somewhere
for this to work?

Yeah, they should be accessible in the client page, the problem could be
that the page events for the master and the page execute at diffrent times.
- Try overriding the OnInit() event in the master, and assign the properties
there.
 
You are correct. I just found this document:

http://msdn2.microsoft.com/en-us/library/dct97kc3.aspx

It lists the order of the Init/Load/PreRender events for Master and
Content pages. There is no PreInit, and the Master's OnLoad event
happens AFTER the Content page.

By moving everything into the Page_Init event, I have solved my
problems.

Thanks for the help!

Kyle
 
Back
Top