Что-то вроде этого:
private int CustomerID
{
get
{
if( Session["CustomerID"] != null )
return Convert.ToInt32( Session["CustomerID"] );
else
return 0;
}
set { Session["CustomerID"] = value; }
}
РЕДАКТИРОВАТЬ:
Альтернативой может быть что-то вроде этого:
public class Persist<T>
{
private string ObjectName;
public Persist( string Name )
{
ObjectName = Name;
}
public T Get()
{
return (T)(HttpContext.Current.Session[ObjectName]);
}
public void Set(T value)
{
HttpContext.Current.Session[ObjectName] = value;
}
}
показано, завернутый в простой класс Singleton.
public class SV
{
private static readonly SV instance = new SV( );
public Persist<DateTime> FiscalDate;
public Persist<decimal> Revenue;
private SV( )
{
FiscalDate = new Persist<DateTime>( "FiscalDate" );
Revenue = new Persist<decimal>( "Revenue" );
}
public static SV Instance
{
get
{
return instance;
}
}
}
Использование, к сожалению, немного многословно.
protected void Page_Load( object sender, EventArgs e )
{
if( !Page.IsPostBack )
{
SV.Instance.Revenue.Set( 1234567890M );
SV.Instance.FiscalDate.Set( new DateTime( 2011, 3, 15 ) );
}
}
protected void Button1_Click( object sender, EventArgs e )
{
DateTime when = SV.Instance.FiscalDate.Get( );
decimal amount = SV.Instance.Revenue.Get( );
}