Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I just want to ask how to store and retrieve session to/from master page. say, i have a login page. upon validation, login page stores a data (from database) to master page, and then content pages can access the session stored in master page. any sample codes?

[updates] Here's the scenario. in login page, users are being sorted to permissions. user types [a], [b], [c]. in master page, there are 3 different div menus. the div menus are only granted to the following user types:

[a] - div menu 1
[b] - div menu 2
[c] - div menus 1, 2, 3

since the master page contains all div menus, how can i disable all other menus according to permissions.
Posted
Updated 3-Aug-13 20:28pm
v2

Session is not stored in your master page, it is stored in memory on the server. Therefore, any page can access it.

To store information in the Session use
C#
Session["SomeVariable"] = "SomeValue";


and to get data from the Session use
C#
String someValue = Session["SomeVariable"];
 
Share this answer
 
Session can be set anywhere and can be accessed anywhere in application.

So, if you set the Session in Master Page, it can also be accessed from Content page.
There are no restriction and limitations.
 
Share this answer
 
Following code is used for storing a value to session:

C#
//Storing yourValue in Session
Session["yourValue"] = textboxUserName.Text;

Now, let see how we can retrieve values from Session

//Check weather session variable null or not
if (Session["yourValue"] != null)
{
//Retrieving yourValue from Session
label1.Text = "Welcome : " + Session["yourValue"];
}
else
{
//Do Something else
}


Following Example shows how to store a DataSetin session:

C#
//Storing dataset on Session
Session["DataSet"] = _dataSetObject;


and following code shows how we can retrieve that value from the session:

C#
//Check weather session variable null or not
if (Session["DataSet"] != null)
{
//Retrieving value from Session
DataSet _ds= (DataSet)Session["DataSet"];
}
else
{
//Do Something else
}
 
Share this answer
 
Session is independent of MasterPage or ContentPage. You can set the values into Session variables throughout your application from any page and once its set you can access that value in any page.

So, once when you handle login you can simply add -
C#
Session["SessionKey"] = YourValue;


For more details you can refer any link -
ASP.NET Session State Overview[^]
Exploring Session in ASP.NET[^]

Hope it helps.
Thanks
 
Share this answer
 
v2
I just added runat="server" and switch those in master page .cs file. thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900