473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

SessionStatic

MRe
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

Aug 26 '07 #1
5 1357
I don't think it is possible. Static variables live in the application
scope.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"MRe" <m.r.e.@.d.u.b.l.i.n...i.ewrote in message
news:ug*************@TK2MSFTNGP06.phx.gbl...
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

Aug 26 '07 #2
MRe
Awh, darn it!

Well, thank you for the response Eliyahu,

Kind regards,
Eliott

"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote in
message news:Or**************@TK2MSFTNGP02.phx.gbl...
>I don't think it is possible. Static variables live in the application
scope.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"MRe" <m.r.e.@.d.u.b.l.i.n...i.ewrote in message
news:ug*************@TK2MSFTNGP06.phx.gbl...
>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?
<snip>
Aug 26 '07 #3
MRe wrote:
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 )
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.

--
Göran Andersson
_____
http://www.guffa.com
Aug 26 '07 #4
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

"MRe" <m.r.e.@.d.u.b.l.i.n...i.ea écrit dans le message de news:
ug*************@TK2MSFTNGP06.phx.gbl...
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

Aug 27 '07 #5
MRe
Yes, very cool. This works great for me.

Thank you very much Patrice,
Kind regards,
Eliott

"Patrice" <http://www.chez.com/scribe/wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
You could expose this variable as static property and still store the
underlying value for this property as a session variable.
<snip>
Patrice

"MRe" <m.r.e.@.d.u.b.l.i.n...i.ea écrit dans le message de news:
ug*************@TK2MSFTNGP06.phx.gbl...
>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>
Aug 29 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.