Hello Burton,
You must first get the current http context. You do that by:
System.Web.HttpContext context = System.Web.HttpContext.get_Current();
Get the session state with:
System.Web.SessionState.HttpSessionState state = context.get_Session();
Now you can eg. set the timeout etc..
state.set_Timeout( 15 );
Add two variables:
state.Add("One", new String("One Hello") );
state.Add("Two", new String("Two here") )
If you want to read the variable:
System.Web.HttpContext context = System.Web.HttpContext.get_Current();
System.Web.SessionState.HttpSessionState state = context.get_Session();
this.Label1.set_Text( ""+state.get_Item("Two") );
Example:
Lets say you have an ASP.Net page with a label called "label1" and a button
called "Button1" with a click handler.
import System.Collections.*;
import System.ComponentModel.*;
import System.Data.*;
import System.Drawing.*;
import System.Web.*;
import System.Web.SessionState.*;
import System.Web.UI.*;
import System.Web.UI.WebControls.*;
import System.Web.UI.HtmlControls.*;
/**
* Summary description for WebForm1.
*/
public class WebForm1 extends System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
private System.Web.HttpContext context;
private System.Web.SessionState.HttpSessionState state;
private void Page_Load(Object sender, System.EventArgs e)
{
// Put user code to initialize the page here
context = System.Web.HttpContext.get_Current();
state = context.get_Session();
state.set_Timeout( 15 ); // Holds a state for 15 minutes.
state.Add("Value", new String("Hello") );
state.Add("Two", new String("Two here") );
}
#region Web Form Designer generated code
protected void OnInit(System.EventArgs e)
{
InitializeComponent();
super.OnInit(e);
}
/**
* Required method for Designer support - do not modify
* the contents of this method with the code editor.
*/
private void InitializeComponent()
{
this.Button1.add_Click( new
System.EventHandler(this.Button1_Click) );
this.add_Load( new System.EventHandler(this.Page_Load) );
}
#endregion
private void Button1_Click (Object sender, System.EventArgs e)
{
System.Web.HttpContext context =
System.Web.HttpContext.get_Current();
System.Web.SessionState.HttpSessionState state =
context.get_Session();
this.Label1.set_Text( ""+state.get_Item("Two") );
}
}
Lars-Inge Tønnessen
www.larsinge.com