6.2. Maintaining Information about a User Throughout a Session
Problem
You want to make personalized information available to the users of your application for as long as each remains active, without having to access a database each time the information is needed and regardless of the number of pages traversed.
Solution
Create a class in which to store the personalized data, instantiate
the class and load the data, store the data object in the
Session object, and then access the data from the
Session object as required.
In the code-behind class for your ASP.NET pages that need access to the data, use the .NET language of your choice to:
Check to see if the object used to store the personalized data exists in the
Sessionobject.If the object exists, retrieve the object from
Session. If the object does not exist, instantiate the class used for the personalized data and store it in theSessionobject.Use the data as required in your application.
A simple example that illustrates this solution is shown in Examples
Example 6-6 through Example 6-10.
The example uses the class shown in Example 6-6
(CH06PersonalDataVB for VB) and Example 6-7 (CH06PersonalDataCS for C#)
to provide a container for some simple personalization data. This
class contains properties for each of the data items and a default
constructor.
Figure 6-2 shows a simple form that
we’ve created for viewing the current contents of
the personalization data stored in the Session object and for entering new session state data ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access