Hi, I am using a masterpage that runs an init routine that loads sql server session variables. I am wanting to access the session variables in the content pages. What I am seeing is that the master page is being processed before the content page which
so there may not be any way to get to these variables. Is there anyway or will I need to run the init routine on all of my content pages? Since the init needs to run for every page load I would like to put it in the master page so the developers will not
need to remember to alway run the init routine on each one of the content pages.
Session is not master/content page specific or even page specific. Once you've put something in session, for a given user's session, any page, or master or content page, should be able to access it with no problem. You should be able to have your master page
call the Init method from Page_Init or Page_Load event and then your content pages can access the session variables at any point later in the page lifecycle. If you're having problems doing this then tell us at what point in your master pages code you are
setting the session variables and post that code and also at what point in the content page you are trying to access them and the code you are using to try and access them.
If this post answered your question please remember to 'Mark as Answer'!
Well, first of all you don't need to do Master.Session because when you add a session variable it isn't specific to the master page it is global across all pages for that user. So you can access it just by doing Session["variablename"] from any page anywhere
in your site for that user's session. The second thing is that the page's Page_Load gets called before the master page's Page_Load. So your value hasn't been added to session at the point that you go through your page's Page_Load and that is why you can't
get the value. You need to add the variables either in your Master Page's Page_Init so it is available when your page goes through Page_Load or you can leave it in Page_Load of your master page and then access it further on in the life cycle of your page like
at Page_LoadComplete or Page_PreRender or something. It will work fine as long as when you access it form the Page it is after you have already added it in the master page.
If this post answered your question please remember to 'Mark as Answer'!
Marked as answer by ssmith1120 on Mar 14, 2007 02:35 PM
ssmith1120
Member
5 Points
39 Posts
Using session from master page in content page
Mar 13, 2007 09:09 PM|LINK
Hi, I am using a masterpage that runs an init routine that loads sql server session variables. I am wanting to access the session variables in the content pages. What I am seeing is that the master page is being processed before the content page which so there may not be any way to get to these variables. Is there anyway or will I need to run the init routine on all of my content pages? Since the init needs to run for every page load I would like to put it in the master page so the developers will not need to remember to alway run the init routine on each one of the content pages.
Thanks for your help.
bpag
Contributor
3517 Points
533 Posts
Re: Using session from master page in content page
Mar 14, 2007 02:21 AM|LINK
ssmith1120
Member
5 Points
39 Posts
Re: Using session from master page in content page
Mar 14, 2007 01:29 PM|LINK
Thanks bpag,
In the master page in the page_load section I am doing this:
protected
void Page_Load(object sender, EventArgs e){
Session.Add(
"steve", "hello world");}
Next in the content page I am doing this:
protected
void Page_Load(object sender, EventArgs e){
Response.Write(
"here is session: " + Master.Session["steve"]);}
Here is what I am getting when the page loads:
here is session:
I need to get the session that I have set in the master page in the content page and just can't figure out the way to do it.
thanks,
bpag
Contributor
3517 Points
533 Posts
Re: Using session from master page in content page
Mar 14, 2007 01:40 PM|LINK
ssmith1120
Member
5 Points
39 Posts
Re: Using session from master page in content page
Mar 14, 2007 02:33 PM|LINK