Tuesday, May 16, 2006

Smart refence state class for web apps

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;

public partial class _Default : System.Web.UI.Page
{
MyState _state = null;

protected void Page_Load(object sender, EventArgs e)
{
this._state = MyState.Init(this.Session);
}
protected void Button1_Click(object sender, EventArgs e)
{
this._state.Antal += 1;
_tbAntal.Text = this._state.Antal.ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
this._state.Antal += 3;
_tbAntal.Text = this._state.Antal.ToString();
}
}

class MyState
{
const string _name = "{1374BFDE-B051-4fa3-8A7A-E506233B2AB6}";

public int Antal = 0;

public static MyState Init( HttpSessionState state )
{
MyState s = null;
if(state[_name] == null)
{
s = new MyState();
state[_name] = s;
}
else
s = state[_name] as MyState;

return s;
}
}

No comments:

Lazzes Smart Home - 2023

Here is an overview of the components in the Smart Home as of now. More stuff than I tought :-)