Retrieving Session Data from a Base Page Class

D

David Young

I normally work on WinForms projects, but have been recently thrust into
this arena and had a questions concering access to objects stored in
session.

We've apparently got an object (custom class) that's stored in session for
each user of our app. This object provides information related to the use
and their environment, as well as default data used in the system. One of
these items is a reference to the stylesheet used for that user.

Rather than have each page in the site retrieve the object and process the
stylesheet reference, I was thinking about having all pages inherit from a
base page class that we write, that would automatically pull the object from
session, get the stylesheet reference and embed the <link> tag in the header
of the page.

Is that do able? Is there something else that I should be looking at doing?
Any suggestions?

Thanks, Dave
 
K

Kevin Spencer

Use HttpContext.Current.Session.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
G

Guest

Yes, you should be putting anything repetitive in a BasePage class derived
from Page. Thet way the BasePage can be concerned with things like
stylesheets.

I've even implemented asynchronous page handling in my BasePage without
having to modify any code in the derived Pages.
 
D

David Young

Thanks,
Now, I see that there is no way to access the documents stylesheet directly.
So, would I have to override the pre init and write out my <link> tag there?

Dave
 
G

Guest

My situation may be slightly different than yours in that my application has
just one page and I dynamically load user controls based on user input,
security settings, etc.

The first part of my Page.aspx looks something like this;

<%@ Page language="c#" Codebehind="Page.aspx.cs" AutoEventWireup="false"
Inherits="Portal.Page" %>
<HTML>
<HEAD>
<title><%=PageTitle%></title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<%=OutputCss()%>
</HEAD>
....
</HTML>

Where OutputCss is a member of BasePage;

protected string OutputCss()
{
// Generate a bunch of link tags or even <style> elements
// (actually you could return anything that's valid in the <head>)

return aBunchOfLinkTags;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top