M
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 )
[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
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 )
[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