views:

503

answers:

2

In my MasterPage I am setting a session variable to some value, that I got from the database. The value is shown on the footer of every page, that's why it is in the MasterPage.

protected void Page_Load(object sender, EventArgs e)
{        
    Session["TODAY"] = value_from_DB;
}

Later on, I want to use this value on other pages, but Session["TODAY"] is null, while its value is shown on the footer.

How can I access Session values in pages, the value is set in the masterpage ?

A: 

Yes, you can use a session value on a page that is set in a MasterPage.

Check to make sure that Session["TODAY"] never is set to null anywhere else in your application. Most likely you are overwriting this value later in the page's life cycle.

Andrew Hare
+3  A: 

As far as I know, this will be because the content page's Page_Load method executes before the master page's Page_Load method, so you're using a session variable before it's created.

IanT8
In other words: know and understand your page lifecycle.
chris
@IanT8 you are right- @dbrmr see here- http://msdn.microsoft.com/en-us/library/dct97kc3.aspx
RichardOD