The Property Page.Session

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Good day,

I have a question about the Page.Session property used in ASP.NET

This property is defined as follows:
public virtual HttpSessionState Session {get;}

and HttpSessionState is a sealed class derived from System.Object and it has
two indexers one integer and on string. For example the following code can be
used to set a Session variable in a web page.

Page.Session["myPage"] = new aClass();

and then it can be retrieved as follows:
aClass ac = (aClass)Page.Session["myPage"];

I will really help if someone could explain how this is possible.
Page.Session is a readonly property afterall. Another thing I don't
understand is that how can you treat Page.Session as an indexer?

I appreciate any clarification.
 
Page.Session refers to the Session object of the current HttpContext, as
defined by HttpContext.Current.Session.

1. The Page.Session property is read only. You cannot change the
HttpContext.Session object associated with the page. That does not mean
that you can't change the properties of the Session object, only that you
can't change which Session object goes with the page.

2. Session is not an indexer, but it has an indexer - two indexers,
actually. One indexer that takes an integer parameter and returns the
Session.Items[value] object.

There is a second indexer that takes a string and, internally, finds the
Session.Item that matches the parameter string and then returns the object
associated with that item.

3. Indexers can be read only, and provide the behaviors as listed above,
or, as in the case of the indexers of the Session object, can be read/write.
When assigning a value, the indexer identifies the appropriate Session.Item
based on the integer index or the string name, creates it if it doesn't
already exist, and assigns the value to it.

HTH

DalePres
MCAD, MCDBA, MCSE
 
Got it. I appreciate your help.

DalePres said:
Page.Session refers to the Session object of the current HttpContext, as
defined by HttpContext.Current.Session.

1. The Page.Session property is read only. You cannot change the
HttpContext.Session object associated with the page. That does not mean
that you can't change the properties of the Session object, only that you
can't change which Session object goes with the page.

2. Session is not an indexer, but it has an indexer - two indexers,
actually. One indexer that takes an integer parameter and returns the
Session.Items[value] object.

There is a second indexer that takes a string and, internally, finds the
Session.Item that matches the parameter string and then returns the object
associated with that item.

3. Indexers can be read only, and provide the behaviors as listed above,
or, as in the case of the indexers of the Session object, can be read/write.
When assigning a value, the indexer identifies the appropriate Session.Item
based on the integer index or the string name, creates it if it doesn't
already exist, and assigns the value to it.

HTH

DalePres
MCAD, MCDBA, MCSE

Nad said:
Good day,

I have a question about the Page.Session property used in ASP.NET

This property is defined as follows:
public virtual HttpSessionState Session {get;}

and HttpSessionState is a sealed class derived from System.Object and it
has
two indexers one integer and on string. For example the following code can
be
used to set a Session variable in a web page.

Page.Session["myPage"] = new aClass();

and then it can be retrieved as follows:
aClass ac = (aClass)Page.Session["myPage"];

I will really help if someone could explain how this is possible.
Page.Session is a readonly property afterall. Another thing I don't
understand is that how can you treat Page.Session as an indexer?

I appreciate any clarification.
 
Back
Top