Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

SessionStatic

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MRe - 26 Aug 2007 04:22 GMT
Hi,

 Writing an ASP.NET page that makes use of a static class in a Class
Library and was..

[Short version]

 ..wondering, is it possible to make a static field, static per session.
I've searched around and cannot find anything specific, (the search term I'm
using seems to return every page on the internet though :P )

[Medium version]

 Something like..

   [SessionStatic]
   public static string Variable;
   ..
   Variable = "something";

 ..as a replacement for..

   Context.Session["Variable"] = "something";

[Long version]

 Sorry if there are any syntax errors in the example below, (I've just
thrown it together now), for the size of this post, and for the contrived
example. (Also for any word-warping on code lines)

 I have a class library (WebLib) that I'm loading into an ASP.NET page
using..

   <%@ Register TagPrefix="web" Namespace="MyNS.WebLib" Assembly="WebLib"
%>

 ..This library has a static class, (as an example)..

   public static class Web
   {
     public static string Name;
   }

 ..and a control..

   public class html : Control
   {
     // Populate Web.Name with the address query string if getname is true
     public bool getname { set { if(value == true) Web.Name =
Context.Request.QueryString["name"]; } }
     // Output a Hi 'name' message at the top of every page
     protected override void Render(HtmlTextWriter writer)
     {
       writer.Write("<html><body>Hi " + Web.Name + "</br>"
       base.Render(writer);
       writer.Write("</body></html>");
     }
   }

 ..If I was to use this in one web page (like a login page), where I
'getname' using..

   <%@ Page Language="C#" AutoEventWireup="false"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
   <%@ Register TagPrefix="web" Namespace="MyNS.WebLib" Assembly="WebLib"
%>
   <!-- About to getname -->
   <web:html runat="server" getname="true">
     <!-- todo -->
   </web:html>

 ..and another page that's been navigated to after the previous [login]
page (with no 'getname', but still using the saved name in Web.Name)..

   <%@ Page Language="C#" AutoEventWireup="false"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
   <%@ Register TagPrefix="web" Namespace="MyNS.WebLib" Assembly="WebLib"
%>
   <!-- Not about to getname -->
   <web:html runat="server">
     <!-- todo -->
   </web:html>

 ..I would want the same name to be output. But as it's a static variable,
if a new user went on to the [login] page, their name would overwrite the
previous (both users would see this new name).

 I have been able to get around this by having a static Web.Context, and
using using Web.Context.Session["Name"], but was wondering if it's possible
to mark the static variable with an attribute (or similar), that would make
it a session variable. As said in the medium version..

   public static class Web
   {
     [SessionStatic]
     public static string Name;
   }

 ..Which would just be a little more hip

Hope someone can help,
Thank you,
Kind regards,
Eliott
Eliyahu Goldin - 26 Aug 2007 09:39 GMT
I don't think it is possible. Static variables live in the application
scope.

Signature

Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net

> Hi,
>
[quoted text clipped - 100 lines]
> Kind regards,
> Eliott
MRe - 26 Aug 2007 19:21 GMT
Awh, darn it!

Well, thank you for the response Eliyahu,

Kind regards,
Eliott

>I don't think it is possible. Static variables live in the application
>scope.
[quoted text clipped - 3 lines]
>>  Writing an ASP.NET page that makes use of a static class in a Class
>> Library and was..
<snip>
>> ..wondering, is it possible to make a static field, static per session?
<snip>
Göran Andersson - 26 Aug 2007 23:04 GMT
> Hi,
>
[quoted text clipped - 6 lines]
> I've searched around and cannot find anything specific, (the search term I'm
> using seems to return every page on the internet though :P )

There is no session static variables.

You can make thread static variables, but they are not very useful. A
thread is not specific for a session, as the threads are pooled. Also, a
 thread static variable is only initialised for the first thread, for
the other threads the value is undefined.

Signature

Göran Andersson
_____
http://www.guffa.com

Patrice - 27 Aug 2007 10:12 GMT
You could expose this variable as static property and still store the
underlying value for this property as a session variable.

This way you have both a strongly typed "session access" object but you
avoid having to have your static data shared by all users (which be be the
case for a true static variable).

---
Patrice

> Hi,
>
[quoted text clipped - 100 lines]
> Kind regards,
> Eliott
MRe - 29 Aug 2007 02:18 GMT
Yes, very cool. This works great for me.

Thank you very much Patrice,
Kind regards,
Eliott

> You could expose this variable as static property and still store the
> underlying value for this property as a session variable.
<snip>
> Patrice
>
>> Hi,
>>
>>  Writing an ASP.NET page that makes use of a static class in a Class
>> Library and was..
<snip>
>>  ..wondering, is it possible to make a static field, static per session.
>> I've searched around and cannot find anything specific, (the search term
>> I'm using seems to return every page on the internet though :P )
<snip>

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.